1 / 107

Graph

Graph. Charles Lin. Agenda. Graph Representation DFS B FS Dijkstra A* Search Bellman-Ford Floyd- Warshall Iterative? Non-iterative? MST Flow – Edmond-Karp. Graph Representation. Adjacency Matrix bool way[100][100]; cin >> i >> j; way[i][j] = true;. Graph Representation.

Download Presentation

Graph

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Graph Charles Lin

  2. Agenda • Graph Representation • DFS • BFS • Dijkstra • A* Search • Bellman-Ford • Floyd-Warshall • Iterative? Non-iterative? • MST • Flow – Edmond-Karp

  3. Graph Representation • Adjacency Matrix • bool way[100][100]; • cin >> i >> j; • way[i][j] = true;

  4. Graph Representation • Adjacency Linked List • vector<int> v[100]; • cin >> i >> j; • v[i].push_back(j);

  5. Graph Representation • Edge List struct Edge { intStart_Vertex, End_Vertex; } edge[10000]; cin >> edge[i].Start_Vertex >> edge[i].End_Vertex;

  6. Graph Representation

  7. Graph Theory 2 B D 10 A 8 4 1 7 9 C E 3 2

  8. Graph Theory • Mission: To go from Point A to Point E • How?

  9. Depth First Search (DFS) • Structure to use: Stack • See “The House of Santa Claus”

  10. Depth First Search (DFS) 2 B D 10 A 8 4 1 7 9 C E 3 2

  11. Depth First Search (DFS) 2 B D 10 A 8 4 1 7 9 C E 3 2

  12. Depth First Search (DFS) 2 B D 10 A 8 4 1 7 9 C E 3 2

  13. Depth First Search (DFS) 2 B D 10 A 8 4 1 7 9 C E 3 2

  14. Depth First Search (DFS) 2 B D 10 A 8 4 1 7 9 C E 3 2

  15. Depth First Search (DFS) 2 B D 10 A 8 4 1 B is visited already 7 9 C E 3 2

  16. Depth First Search (DFS) 2 B D 10 A 8 4 1 7 9 C E 3 2

  17. Depth First Search (DFS) 2 B D 10 A 8 4 1 7 9 C E 3 2

  18. Depth First Search (DFS) 2 B D 10 A 8 4 1 7 9 C E 3 2

  19. Depth First Search (DFS) 2 B D 10 A 8 4 1 7 9 C E 3 2

  20. Depth First Search (DFS) 2 B D 10 A 8 4 1 7 9 C E 3 2 We find a path from Point A to Point E, but ……

  21. Depth First Search (DFS) int main() { memset(Visited, 0, sizeof(Visited)); // Input Handling (GOAL = ?) DFS(0); } stack<int> s; bool Visited[MAX]; void DFS(int u) { s.push(u); if (u == GOAL) { for (int x = 0; x < s.size(); x++) printf(“%d “, s[x]); return ; } for (int x = 0; x < MAX; x++) if (!Visited[x]) { Visited[x] = true; DFS(x); Visited[x] = false; } s.pop();} Very Important It needs to be restored for other points to traverse, or the path may not be found

  22. Depth First Search (DFS) • Usage: • Finding a path from start to destination (NOT RECOMMENDED – TLE) • Topological Sort (T-Sort) • Strongly-Connected Component (SCC)

  23. Depth First Search (DFS) • Topological Sort • Directed Acyclic Graph (DAG) • Find the order of nodes such that for each node, every parent is before that node on the list • Dump Method: Check for root of residual graph (Do it n times) • Better Method: Reverse of finishing time

  24. Depth First Search (DFS) T-Sort: 2 B D 10 A 8 4 1 7 9 C E 3 2 Cycle Exists !!!

  25. Depth First Search (DFS) T-Sort: B D A C E OK

  26. Depth First Search (DFS) T-Sort: B D A C E Things in output stack: NONE

  27. Depth First Search (DFS) T-Sort: B D A C E Things in output stack: NONE

  28. Depth First Search (DFS) T-Sort: B D A C E Things in output stack: NONE

  29. Depth First Search (DFS) T-Sort: B D A C E Things in output stack: NONE

  30. Depth First Search (DFS) T-Sort: B D A C E Things in output stack: NONE

  31. Depth First Search (DFS) T-Sort: B D A C E Things in output stack: E

  32. Depth First Search (DFS) T-Sort: B D A C E Things in output stack: E, D

  33. Depth First Search (DFS) T-Sort: B D A C E Things in output stack: E, D, B

  34. Depth First Search (DFS) T-Sort: B D A C E Things in output stack: E, D, B

  35. Depth First Search (DFS) T-Sort: B D A C E Things in output stack: E, D, B

  36. Depth First Search (DFS) T-Sort: B D A C E Things in output stack: E, D, B

  37. Depth First Search (DFS) T-Sort: B D A C E Things in output stack: E, D, B

  38. Depth First Search (DFS) T-Sort: B D A C E Things in output stack: E, D, B, C

  39. Depth First Search (DFS) T-Sort: B D A C E Things in output stack: E, D, B, C, A T-Sort Output: A, C, B, D, E

  40. Depth First Search (DFS) • Strongly Connected Components • Directed Graph • Find groups of nodes such that in each group, every node has an edge to every other node

  41. Depth First Search (DFS) • Strongly Connected Components • Method: DFS twice • Do DFS on Graph G, record finishing time of node • Do DFS on Graph GT, by decreasing finish time • Output the vertices

  42. Depth First Search (DFS) • Strongly Connected Components • Note: After grouping the vertices, the new nodes form a Directed Acyclic Graph • Problem: Where’s the back button

  43. Breadth First Search (BFS) • Structure to use: Queue

  44. Breadth First Search (BFS) Things in queue: A, C 3 A, B 10 2 B D 10 A 8 4 1 7 9 C E 3 2

  45. Breadth First Search (BFS) Things in queue: A, B 10 A, C, E5 A, C, B 7 A, C, D 8 2 B D 10 A 8 4 1 7 9 C E 3 2

  46. Breadth First Search (BFS) Things in queue: A, C, E5 A, C, B 7 A, C, D 8 A, B, C 11 A, B, D 12 2 B D 10 A 8 4 1 7 9 C E 3 2

  47. Breadth First Search (BFS) Things in queue: A, C, B 7 A, C, D 8 A, B, C 11 A, B, D 12 2 B D 10 A 8 4 1 7 9 C E 3 2 We are done !!!

  48. Breadth First Search (BFS) • Application: • Finding shortest path • Flood-Fill

  49. Dijkstra • Structure to use: Priority Queue • Consider the past • The length of path visited so far • No negative edges allowed

  50. Dijkstra Things in priority queue: A, C 3 A, B 10 2 B D 10 A 8 4 1 7 9 Current Route: A C E 3 2

More Related