Graph Charles Lin
genda Graph Representation FS BFS ijkstra * Search Bellman-Ford Floyd-Warshall Iterative? Non-iterative? MST Flow Edmond-Karp
Graph Representation djacency Matrix bool way[100][100]; cin >> i >> j; way[i][j] = true;
Graph Representation djacency Linked List vector<int> v[100]; cin >> i >> j; v[i].push_back(j);
Graph Representation Edge List struct Edge { int Start_Vertex, End_Vertex; } edge[10000]; cin >> edge[i].start_vertex >> edge[i].end_vertex;
Graph Representation
Graph Theory 10 B 1 4 8 7 9 3 C E
Graph Theory Mission: To go from Point to Point E How?
epth First Search (FS) Structure to use: Stack
epth First Search (FS) 10 B 1 4 8 7 9 3 C E
epth First Search (FS) 10 B 1 4 8 7 9 3 C E
epth First Search (FS) 10 B 1 4 8 7 9 3 C E
epth First Search (FS) 10 B 1 4 8 7 9 3 C E
epth First Search (FS) 10 B 1 4 8 7 9 3 C E
epth First Search (FS) 10 B 1 4 8 7 9 B is visited already 3 C E
epth First Search (FS) 10 B 1 4 8 7 9 3 C E
epth First Search (FS) 10 B 1 4 8 7 9 3 C E
epth First Search (FS) 10 B 1 4 8 7 9 3 C E
epth First Search (FS) 10 B 1 4 8 7 9 3 C E
epth First Search (FS) 10 B 1 4 8 7 9 3 C E We find a path from Point to Point E, but
epth First Search (FS) stack<int> s; bool Visited[MX]; void FS(int u) { } s.push(u); if (u == GOL) { } for (int x = 0; x < s.size(); x++) return ; printf( %d, s[x]); for (int x = 0; x < MX; x++) if (!Visited[x]) { } s.pop(); Visited[x] = true; FS(x); Visited[x] = false; int main() { memset(visited, 0, sizeof(visited)); // Input Handling (GOL =?) FS(0); } Very Important It needs to be restored for other points to traverse, or the path may not be found
epth First Search (FS) Usage: Finding a path from start to destination (NOT RECOMMENE TLE) Topological Sort (T-Sort) Strongly-Connected Components (SCC) etect cycles
epth First Search (FS) Topological Sort irected cyclic Graph (G) Find the order of nodes such that for each node, every parent is before that node on the list umb method: Check for root of residual graph (o it n times) Better Method: Reverse of finishing time Start from all vertices!
epth First Search (FS) T-Sort: 10 B 1 4 8 7 9 3 C E Cycle Exists!!!
epth First Search (FS) T-Sort: B C E OK
epth First Search (FS) T-Sort: B C E Things in output stack: NONE
epth First Search (FS) T-Sort: B C E Things in output stack: NONE
epth First Search (FS) T-Sort: B C E Things in output stack: NONE
epth First Search (FS) T-Sort: B C E Things in output stack: NONE
epth First Search (FS) T-Sort: B C E Things in output stack: E
epth First Search (FS) T-Sort: B C E Things in output stack: E,
epth First Search (FS) T-Sort: B C E Things in output stack: E,, B
epth First Search (FS) T-Sort: B C E Things in output stack: E,, B
epth First Search (FS) T-Sort: B C E Things in output stack: E,, B
epth First Search (FS) T-Sort: B C E Things in output stack: E,, B
epth First Search (FS) T-Sort: B C E Things in output stack: E,, B
epth First Search (FS) T-Sort: B C E Things in output stack: E,, B, C
epth First Search (FS) T-Sort: B C E Things in output stack: E,, B, C, T-Sort Output:, C, B,, E
epth First Search (FS) Strongly Connected Components irected Graph Find groups of nodes such that in each group, every node has a path to every other node
epth First Search (FS) Strongly Connected Components Method: FS twice (Kosaraju) o FS on graph G from all vertices, record finishing time of node Reverse direction of edges o FS on Graph G from all vertices sorted by decreasing finishing time Each FS tree is an SCC
epth First Search (FS) Strongly Connected Components Note: fter grouping the vertices, the new nodes form a irected cyclic Graph
Breadth First Search (BFS) Structure to use: Queue
10 B Breadth First Search (BFS) Things in queue:, C 3, B 10 1 4 8 7 9 3 C E
10 B Breadth First Search (BFS) Things in queue:, B 10, C, E 5, C, B 7, C, 8 1 4 8 7 9 3 C E
10 B Breadth First Search (BFS) Things in queue:, C, E 5, C, B 7, C, 8, B, C 11, B, 1 1 4 8 7 9 3 C E
10 B Breadth First Search (BFS) Things in queue:, C, B 7, C, 8, B, C 11, B, 1 1 4 8 7 9 3 C E We are done!!!
Breadth First Search (BFS) pplication: Finding shortest path Flood-Fill
ijkstra Structure to use: Priority Queue Consider the past The length of path visited so far No negative edges allowed Maintain known vertices t each step: Take the unknown vertex with smallest overall estimate
10 ijkstra B Things in priority queue:, C 3, B 10 1 4 8 7 9 Current Route: 3 C E
10 ijkstra B Things in priority queue:, C, E 5, C, B 7, B 10, C, 11 1 4 8 7 9 Current Route:, C 3 C E
10 ijkstra B Things in priority queue:, C, B 7, B 10, C, 11 1 4 8 7 9 Current Route:, C, E 3 C E We find the shortest path already!!!
* Search Structure to use: Priority Queue Consider the past + future How to consider the future? dmissible Heuristic (Best-Case Prediction: must never overestimate) How to predict?
* Search Prediction 1: isplacement Given coordinates, calculate the displacement of current node to destination sqrt((x x1) + (y y1) ) Prediction : Manhattan istance Given grid, calculate the horizontal and vertical movement (x x1) + (y y1)
* Search est Start - Manhattan istance is used - If there is a tie, consider the one with lowest heuristic first - If there is still a tie, consider in Up, own, Left, Right order
* Search est 1 + 5 = 6 Start (0 + 6 = 6) 1 + 5 = 6 - Manhattan istance is used - If there is a tie, consider the one with lowest heuristic first - If there is still a tie, consider in Up, own, Left, Right order
* Search est + 4 = 6 1 + 5 = 6 Start (0 + 6 = 6) 1 + 5 = 6 - Manhattan istance is used - If there is a tie, consider the one with lowest heuristic first - If there is still a tie, consider in Up, own, Left, Right order
* Search 3 + 3 = 6 est + 4 = 6 1 + 5 = 6 Start (0 + 6 = 6) 1 + 5 = 6 - Manhattan istance is used - If there is a tie, consider the one with lowest heuristic first - If there is still a tie, consider in Up, own, Left, Right order
* Search 4 + 4 = 8 3 + 3 = 6 4 + = 6 est + 4 = 6 1 + 5 = 6 Start (0 + 6 = 6) 1 + 5 = 6 - Manhattan istance is used - If there is a tie, consider the one with lowest heuristic first - If there is still a tie, consider in Up, own, Left, Right order
* Search 4 + 4 = 8 5 + 3 = 8 3 + 3 = 6 4 + = 6 est + 4 = 6 1 + 5 = 6 Start (0 + 6 = 6) 1 + 5 = 6 - Manhattan istance is used - If there is a tie, consider the one with lowest heuristic first - If there is still a tie, consider in Up, own, Left, Right order
* Search 4 + 4 = 8 5 + 3 = 8 3 + 3 = 6 4 + = 6 est + 4 = 6 1 + 5 = 6 Start (0 + 6 = 6) 1 + 5 = 6 + 4 = 6 - Manhattan istance is used - If there is a tie, consider the one with lowest heuristic first - If there is still a tie, consider in Up, own, Left, Right order
* Search 4 + 4 = 8 5 + 3 = 8 3 + 3 = 6 4 + = 6 est + 4 = 6 1 + 5 = 6 3 + 3 = 6 Start (0 + 6 = 6) 1 + 5 = 6 + 4 = 6 3 + 3 = 6 - Manhattan istance is used - If there is a tie, consider the one with lowest heuristic first - If there is still a tie, consider in Up, own, Left, Right order
* Search 4 + 4 = 8 5 + 3 = 8 3 + 3 = 6 4 + = 6 est + 4 = 6 1 + 5 = 6 3 + 3 = 6 Start (0 + 6 = 6) 1 + 5 = 6 + 4 = 6 3 + 3 = 6 - Manhattan istance is used - If there is a tie, consider the one with lowest heuristic first - If there is still a tie, consider in Up, own, Left, Right order
* Search 4 + 4 = 8 5 + 3 = 8 3 + 3 = 6 4 + = 6 est + 4 = 6 1 + 5 = 6 3 + 3 = 6 Start (0 + 6 = 6) 1 + 5 = 6 + 4 = 6 3 + 3 = 6 4 + 4 = 8 - Manhattan istance is used - If there is a tie, consider the one with lowest heuristic first - If there is still a tie, consider in Up, own, Left, Right order
* Search 4 + 4 = 8 5 + 3 = 8 6 + = 8 3 + 3 = 6 4 + = 6 est + 4 = 6 1 + 5 = 6 3 + 3 = 6 Start (0 + 6 = 6) 1 + 5 = 6 + 4 = 6 3 + 3 = 6 4 + 4 = 8 - Manhattan istance is used - If there is a tie, consider the one with lowest heuristic first - If there is still a tie, consider in Up, own, Left, Right order
* Search 4 + 4 = 8 5 + 3 = 8 6 + = 8 7 + 1 = 8 3 + 3 = 6 4 + = 6 est + 4 = 6 1 + 5 = 6 3 + 3 = 6 Start (0 + 6 = 6) 1 + 5 = 6 + 4 = 6 3 + 3 = 6 4 + 4 = 8 - Manhattan istance is used - If there is a tie, consider the one with lowest heuristic first - If there is still a tie, consider in Up, own, Left, Right order
* Search 4 + 4 = 8 5 + 3 = 8 6 + = 8 7 + 1 = 8 8 + = 10 3 + 3 = 6 4 + = 6 est (8 + 0 = 8) + 4 = 6 1 + 5 = 6 3 + 3 = 6 Start (0 + 6 = 6) 1 + 5 = 6 + 4 = 6 3 + 3 = 6 4 + 4 = 8 - Manhattan istance is used - If there is a tie, consider the one with lowest heuristic first - If there is still a tie, consider in Up, own, Left, Right order
* Search 4 + 4 = 8 5 + 3 = 8 6 + = 8 7 + 1 = 8 8 + = 10 3 + 3 = 6 4 + = 6 est (8 + 0 = 8) + 4 = 6 1 + 5 = 6 3 + 3 = 6 Start (0 + 6 = 6) 1 + 5 = 6 + 4 = 6 3 + 3 = 6 4 + 4 = 8 - Manhattan istance is used - If there is a tie, consider the one with lowest heuristic first - If there is still a tie, consider in Up, own, Left, Right order
* Search * Search = Past + Future * Search Future =? ijkstra * Search Past =? Greedy
Graph Theory How about graph with negative edges? Bellman-Ford
Bellman-Ford lgorithm Consider the source as weight 0, others as infinity Count = 0, Improve = true While (Count < n and Improve) { Improve = false Count++ For each edge uv If u.weight + uv distance < v.weight { } Improve = true v.weight = u.weight + uv.distance } If (Count == n) print Negative weight cycle detected Else print shortest path distance
Bellman-Ford lgorithm Time Compleity: O(VE)
Graph Theory ll we have just dealt with is single source How about multiple sources (all sources)? Floyd-Warshall lgorithm
Floyd-Warshall lgorithm 1. Compute on subgraph {1}. Compute on subgraph {1,} 3. Compute on subgraph {1,..., 3} 4.... N. Compute on entire graph one!
Floyd-Warshall lgorithm For (int k = 0; k < MX; k++) { For (int i = 0; i < MX; i++) { For (int j = 0; j < MX; j++) { Path[i][j] = min(path[i][j], Path[i][k] + Path[k][j]); } } }
Iterative? Non-iterative? What is iterative-deepening? Given limited time, f nd the current most optimal solution ijkstra (Shortest 1-step Solution) ijkstra (Shortest -steps Solution) ijkstra (Shortest 3-steps Solution) ijkstra (Shortest n-steps Solution) Suitable in bi-directional search (Faster than 1-way search): only for FS, tricky otherwise
s t
s t
Minimum Spanning Tree (MST) Given an undirected graph, find the minimum cost so that every node has path to other nodes Kruskal s lgorithm Prim s lgorithm
Kruskal s lgorithm Continue Finding Shortest Edge If no cycle is formed add it into the list Construct a parent-child relationship If all nodes have the same root, we are done (path compression) General idea: union-find, may be useful in other situations
Kruskal s lgorithm 4 B 1 7 8 3 C 10 E Node B C E Parent B C E
Kruskal s lgorithm Edge List: BC 1 4 B 1 7 8 3 C 10 E Node B C E Parent B B E
Kruskal s lgorithm Edge List: BC 1 4 B 1 7 8 3 C 10 E Node B C E Parent B B B E
Kruskal s lgorithm Edge List: BC 1 4 B 1 7 8 3 C 10 E Node B C E Parent E
Cycle formed 4 B Kruskal s lgorithm Edge List: BC 1 1 7 8 3 C 10 E Node B C E Parent E
Kruskal s lgorithm Edge List: BC 1 4 B 1 7 8 3 C 10 E Node B C E Parent E
Cycle formed 4 B Kruskal s lgorithm Edge List: BC 1 1 7 8 3 C 10 E Node B C E Parent E
Kruskal s lgorithm Edge List: BC 1 4 B 1 7 8 3 C 10 E Node B C E Parent
Kruskal s lgorithm Path Compression can be done when we find root int find_root(int x) { return (parent[x] == x)? x: return (parent[x] = find_root(parent[x])); }
Prim s lgorithm While not all nodes are visited While the list is not empty and the minimum edge connects to visited node Pop out the edge If the list is empty, print Impossible and return Else Consider popped edge Mark its endpoint as visited dd all its unvisited edges to the list Print the tree
4 B Prim s lgorithm Edge List: C 3 B 4 1 7 8 3 C 10 E
4 B Prim s lgorithm Edge List: CB 1 B 4 C 7 CE 10 1 7 8 3 C 10 E
4 B Prim s lgorithm Edge List: B B 4 C 7 CE 10 1 7 8 3 C 10 E
4 B Prim s lgorithm Edge List: B 4 C 7 E 8 CE 10 1 7 8 3 C 10 E
Cycle formed Prim s lgorithm 4 B Edge List: C 7 E 8 CE 10 1 7 8 3 C 10 E
Cycle formed Prim s lgorithm 4 B Edge List: E 8 CE 10 1 7 8 3 C 10 E
Prim s lgorithm Edge List: CE 10 4 B 1 7 8 3 C 10 E We are done
irected Minimum Spanning Tree We have just dealt with undirected MST. How about directed? Using Kruskal s or Prim s cannot solve directed MST problem How? Chu-Liu/Edmonds lgorithm
Chu-Liu/Edmonds lgorithm For each vertex, find the smallest incoming edge While there is cycle Find an edge entering the cycle with smallest increase Remove the edge pointing to the same node and replace by new edge Print out the tree
Chu-Liu/Edmonds lgorithm 10 B 4 5 7 9 3 C 8 E
Chu-Liu/Edmonds lgorithm 10 B 4 5 7 9 3 C 8 E Cycle formed (B, C, ) => B, C, = 10 = 8 => B, C, = 3 = 1 E => B, C, = 9 4 = 5
Chu-Liu/Edmonds lgorithm 10 B 4 5 7 9 3 C 8 E No cycle formed, we terminate
Chu-Liu/Edmonds lgorithm Using this algorithm may unloop a cycle and create another cycle But since the length of tree is increasing, there will have an end eventually Worst Case is doing n-1 times Time Complexity is O(EV)
Flow graph with weight (capacity) Mission: Find out the maximum flow from source to destination How?
Edmond-Karp o { o BFS on the graph to find maximum flow in the graph dd it to the total flow For each edge in the maximum flow educt the flow just passed } While the flow is not zero;
The End