|
2 | 2 | /*
|
3 | 3 | have the function FarthestNodes(strArr) read strArr which will be an array of hyphenated letters representing paths between those two nodes. For example: ["a-b","b-c","b-d"] means that there is a path from node a to b (and b to a), b to c, and b to d. Your program should determine the longest path that exists in the graph and return the length of that path. So for the example above, your program should return 2 because of the paths a-b-c and d-b-c. The path a-b-c also means that there is a path c-b-a. No cycles will exist in the graph and every node will be connected to some other node in the graph.
|
4 | 4 | */
|
5 |
| - |
6 | 5 | #include <iostream>
|
7 | 6 | #include <string>
|
8 | 7 | #include <vector>
|
9 |
| -#include <stack> |
| 8 | +#include <queue> |
10 | 9 | #include <map>
|
11 | 10 | using namespace std;
|
12 | 11 |
|
| 12 | +/* |
| 13 | +one approach is to treat each node as a possible starting point from where we can expand and perform BFS |
| 14 | +at each step of our BFS we update the cost of each node |
| 15 | +this cost determines the length from the current starting node |
| 16 | +we repeat this process for all the nodes in our graph and keep track of the highest length |
| 17 | +*/ |
| 18 | + |
| 19 | +map <char, int> hashTable; |
13 | 20 |
|
14 | 21 | // structure to represent each node in the graph
|
15 | 22 | struct node
|
16 | 23 | {
|
| 24 | + int cost; |
17 | 25 | char name;
|
18 | 26 | bool visited;
|
19 | 27 | vector <char> neighbors;
|
20 | 28 | };
|
21 | 29 |
|
| 30 | +// method to create the nodes in our graph |
| 31 | +void createGraph(vector <node*>& graph, string values[], int size) |
| 32 | +{ |
| 33 | + int index = 0; |
| 34 | + |
| 35 | + for (int x = 0; x < size; x++) |
| 36 | + { |
| 37 | + char node1 = values[x][0]; |
| 38 | + char node2 = values[x][2]; |
| 39 | + |
| 40 | + // check if the current node is already in our hash table |
| 41 | + // this determines if we need to add it to our graph |
| 42 | + // for the first iteration we add the first 2 letters analyzed |
| 43 | + if (x == 0) |
| 44 | + { |
| 45 | + hashTable[node1] = index; |
| 46 | + |
| 47 | + // making the node |
| 48 | + graph.push_back(new node); |
| 49 | + graph[index]->cost = 0; |
| 50 | + graph[index]->visited = false; |
| 51 | + graph[index]->name = node1; |
| 52 | + index++; |
| 53 | + |
| 54 | + hashTable[node2] = index; |
| 55 | + graph.push_back(new node); |
| 56 | + graph[index]->cost = 0; |
| 57 | + graph[index]->visited = false; |
| 58 | + graph[index]->name = node2; |
| 59 | + index++; |
| 60 | + } |
| 61 | + else |
| 62 | + { |
| 63 | + if (hashTable.count(node1) <= 0) |
| 64 | + { |
| 65 | + hashTable[node1] = index; |
| 66 | + graph.push_back(new node); |
| 67 | + graph[index]->cost = 0; |
| 68 | + graph[index]->visited = false; |
| 69 | + graph[index]->name = node1; |
| 70 | + index++; |
| 71 | + } |
| 72 | + |
| 73 | + if (hashTable.count(node2) <= 0) |
| 74 | + { |
| 75 | + hashTable[node2] = index; |
| 76 | + graph.push_back(new node); |
| 77 | + graph[index]->cost = 0; |
| 78 | + graph[index]->visited = false; |
| 79 | + graph[index]->name = node2; |
| 80 | + index++; |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// method to create the pair connections |
| 87 | +void makeConnections(vector <node*> graph, string values[], int size) |
| 88 | +{ |
| 89 | + for (int x = 0; x < size; x++) |
| 90 | + { |
| 91 | + char node1 = values[x][0]; |
| 92 | + char node2 = values[x][2]; |
| 93 | + |
| 94 | + // making the bidirectional edge connection between the 2 nodes |
| 95 | + graph[hashTable[node1]]->neighbors.push_back(node2); |
| 96 | + graph[hashTable[node2]]->neighbors.push_back(node1); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// method to reset our graph to default values |
| 101 | +void resetGraph(vector <node*> graph) |
| 102 | +{ |
| 103 | + for (int x = 0; x < graph.size(); x++) |
| 104 | + { |
| 105 | + graph[x]->cost = 0; |
| 106 | + graph[x]->visited = false; |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// method that return the farthest node from the specified source node |
| 111 | +int getFarthest(node* source, vector <node*> graph) |
| 112 | +{ |
| 113 | + // in this method we are applying a simple BFS from the source |
| 114 | + queue <node*> list; |
| 115 | + list.push(source); |
| 116 | + |
| 117 | + // keep track of farthest length |
| 118 | + int length = 0; |
| 119 | + |
| 120 | + while (!list.empty()) |
| 121 | + { |
| 122 | + node* current = list.front(); |
| 123 | + list.pop(); |
| 124 | + |
| 125 | + current->visited = true; |
| 126 | + |
| 127 | + // check the neighbors of current node |
| 128 | + for (int x = 0; x < current->neighbors.size(); x++) |
| 129 | + { |
| 130 | + int index = hashTable[current->neighbors[x]]; |
| 131 | + |
| 132 | + // only analyze unvisited neighbors |
| 133 | + if (graph[index]->visited == false) |
| 134 | + { |
| 135 | + // update its cost and compare it to the length |
| 136 | + int newLength = current->cost + 1; |
| 137 | + graph[index]->cost = newLength; |
| 138 | + |
| 139 | + if (newLength > length) |
| 140 | + { |
| 141 | + length = newLength; |
| 142 | + } |
22 | 143 |
|
23 |
| -string FarthestNodes(string strArr[], int size) |
| 144 | + // adding to our queue |
| 145 | + list.push(graph[index]); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + return length; |
| 151 | +} |
| 152 | + |
| 153 | +int FarthestNodes(string strArr[], int size) |
24 | 154 | {
|
| 155 | + int length = 0; |
| 156 | + |
| 157 | + // setting up our graph |
| 158 | + vector <node*> graph; |
| 159 | + createGraph(graph, strArr, size); |
| 160 | + |
| 161 | + // making the connections |
| 162 | + makeConnections(graph, strArr, size); |
| 163 | + |
| 164 | + // in this operation we perform a BFS from every node in our graph |
| 165 | + for (int x = 0; x < graph.size(); x++) |
| 166 | + { |
| 167 | + int currentLength = getFarthest(graph[x], graph); |
| 168 | + |
| 169 | + if (currentLength > length) |
| 170 | + { |
| 171 | + length = currentLength; |
| 172 | + } |
| 173 | + |
| 174 | + // resetting our graph; |
| 175 | + resetGraph(graph); |
| 176 | + } |
25 | 177 |
|
| 178 | + hashTable.clear(); |
| 179 | + |
| 180 | + return length; |
26 | 181 | }
|
27 | 182 |
|
28 | 183 | int main()
|
|
0 commit comments