Presentation is loading. Please wait.

Presentation is loading. Please wait.

Charles Lin. Graph Representation Graph Representation DFS DFS BFS BFS Dijkstra Dijkstra A* Search A* Search Bellman-Ford Bellman-Ford Floyd-Warshall.

Similar presentations


Presentation on theme: "Charles Lin. Graph Representation Graph Representation DFS DFS BFS BFS Dijkstra Dijkstra A* Search A* Search Bellman-Ford Bellman-Ford Floyd-Warshall."— Presentation transcript:

1 Charles Lin

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

3 Adjacency Matrix Adjacency Matrix – bool way[100][100]; – cin >> i >> j; – way[i][j] = true;

4 Adjacency Linked List Adjacency Linked List – vector v[100]; – cin >> i >> j; – v[i].push_back(j);

5 Edge List Edge List struct Edge { int Start_Vertex, End_Vertex; } edge[10000]; cin >> edge[i].Start_Vertex >> edge[i].End_Vertex;

6

7 A B C D E 10 3 1 4 2 2 8 79

8 Mission: To go from Point A to Point E Mission: To go from Point A to Point E How? How?

9 Structure to use:Stack Structure to use:Stack See “The House of Santa Claus” See “The House of Santa Claus”

10 A B C D E 10 3 1 4 2 2 8 79

11 A B C D E 3 1 4 2 2 8 79

12 A B C D E 3 1 4 2 2 8 79

13 A B C D E 3 1 4 2 2 8 79

14 A B C D E 3 1 4 2 2 8 79

15 A B C D E 3 1 4 2 2 8 79 B is visited already

16 A B C D E 10 3 1 4 2 2 8 79

17 A B C D E 3 1 4 2 2 8 79

18 A B C D E 3 1 4 2 2 8 79

19 A B C D E 3 1 4 2 2 8 79

20 A B C D E 3 1 4 2 2 8 79 We find a path from Point A to Point E, but ……

21 stack 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(); } int main() { memset(Visited, 0, sizeof(Visited)); // Input Handling (GOAL = ?) DFS(0); } Very Important It needs to be restored for other points to traverse, or the path may not be found

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

23 Topological Sort 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 A B C D E 10 3 1 4 2 2 8 79 Cycle Exists !!! T-Sort:

25 A B C D E OK T-Sort:

26 A B C D E Things in output stack: NONE T-Sort:

27 A B C D E T-Sort:

28 A B C D E T-Sort:

29 A B C D E T-Sort:

30 A B C D E T-Sort:

31 A B C D E Things in output stack: E T-Sort:

32 A B C D E Things in output stack: E, D T-Sort:

33 A B C D E Things in output stack: E, D, B T-Sort:

34 A B C D E T-Sort:

35 A B C D E T-Sort:

36 A B C D E T-Sort:

37 A B C D E T-Sort:

38 A B C D E Things in output stack: E, D, B, C T-Sort:

39 A B C D E Things in output stack: E, D, B, C, A T-Sort Output: A, C, B, D, E T-Sort:

40 Strongly Connected Components Strongly Connected Components – Directed Graph – Find groups of nodes such that in each group, every node has an edge to every other node

41 Strongly Connected Components Strongly Connected Components – Method: DFS twice – Do DFS on Graph G, record finishing time of node – Do DFS on Graph G T, by decreasing finish time – Output the vertices

42 Strongly Connected Components Strongly Connected Components Note: After grouping the vertices, the new nodes form a Directed Acyclic Graph Note: After grouping the vertices, the new nodes form a Directed Acyclic Graph Problem: Where’s the back button Problem: Where’s the back button

43 Structure to use:Queue Structure to use:Queue

44 A B C D E 10 3 1 4 2 2 8 79 Things in queue: A, C3 A, B10

45 A B C D E 10 3 1 4 2 2 8 79 Things in queue: A, B10 A, C, E5 A, C, B7 A, C, D8

46 A B C D E 10 3 1 4 2 2 8 79 Things in queue: A, C, E5 A, C, B7 A, C, D8 A, B, C11 A, B, D12

47 A B C D E 10 3 1 4 2 2 8 79 Things in queue: A, C, B7 A, C, D8 A, B, C11 A, B, D12 We are done !!!

48 Application: Application: – Finding shortest path – Flood-Fill

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

50 A B C D E 10 3 1 4 2 2 8 79 Things in priority queue: A, C3 A, B10 Current Route: A

51 A B C D E 10 3 1 4 2 2 8 79 Things in priority queue: A, C, E5 A, C, B7 A, B10 A, C, D11 Current Route: A, C

52 A B C D E 10 3 1 4 2 2 8 79 Things in priority queue: A, C, B7 A, B10 A, C, D11 We find the shortest path already !!! Current Route: A, C, E

53 Structure to use:Priority Queue Structure to use:Priority Queue Consider the past + future Consider the past + future How to consider the future? How to consider the future? – Admissible Heuristic (Best-Case Prediction) – How to predict?

54 Prediction 1:Displacement Prediction 1:Displacement – Given coordinates, calculate the displacement of current node to destination – sqrt((x2 – x1) 2 + (y2 – y1) 2 ) Prediction 2:Manhattan Distance Prediction 2:Manhattan Distance – Given grid, calculate the horizontal and vertical movement – (x2 – x1) + (y2 – y1)

55 Prediction 3: Hamming Distance Prediction 3: Hamming Distance – Given pieces and location, find out the number of misplaced pieces

56 Dest Start - Manhattan Distance is used -If there is a tie, consider the one with lowest heuristic first -If there is still a tie, consider in Up, Down, Left, Right order

57 Dest 1 + 5 = 6 Start (0 + 6 = 6)1 + 5 = 6 - Manhattan Distance is used -If there is a tie, consider the one with lowest heuristic first -If there is still a tie, consider in Up, Down, Left, Right order

58 Dest 2 + 4 = 6 1 + 5 = 6 Start (0 + 6 = 6)1 + 5 = 6 - Manhattan Distance is used -If there is a tie, consider the one with lowest heuristic first -If there is still a tie, consider in Up, Down, Left, Right order

59 3 + 3 = 6Dest 2 + 4 = 6 1 + 5 = 6 Start (0 + 6 = 6)1 + 5 = 6 - Manhattan Distance is used -If there is a tie, consider the one with lowest heuristic first -If there is still a tie, consider in Up, Down, Left, Right order

60 4 + 4 = 8 3 + 3 = 64 + 2 = 6Dest 2 + 4 = 6 1 + 5 = 6 Start (0 + 6 = 6)1 + 5 = 6 - Manhattan Distance is used -If there is a tie, consider the one with lowest heuristic first -If there is still a tie, consider in Up, Down, Left, Right order

61 4 + 4 = 85 + 3 = 8 3 + 3 = 64 + 2 = 6Dest 2 + 4 = 6 1 + 5 = 6 Start (0 + 6 = 6)1 + 5 = 6 - Manhattan Distance is used -If there is a tie, consider the one with lowest heuristic first -If there is still a tie, consider in Up, Down, Left, Right order

62 4 + 4 = 85 + 3 = 8 3 + 3 = 64 + 2 = 6Dest 2 + 4 = 6 1 + 5 = 6 Start (0 + 6 = 6)1 + 5 = 62 + 4 = 6 - Manhattan Distance is used -If there is a tie, consider the one with lowest heuristic first -If there is still a tie, consider in Up, Down, Left, Right order

63 4 + 4 = 85 + 3 = 8 3 + 3 = 64 + 2 = 6Dest 2 + 4 = 6 1 + 5 = 63 + 3 = 6 Start (0 + 6 = 6)1 + 5 = 62 + 4 = 63 + 3 = 6 - Manhattan Distance is used -If there is a tie, consider the one with lowest heuristic first -If there is still a tie, consider in Up, Down, Left, Right order

64 4 + 4 = 85 + 3 = 8 3 + 3 = 64 + 2 = 6Dest 2 + 4 = 6 1 + 5 = 63 + 3 = 6 Start (0 + 6 = 6)1 + 5 = 62 + 4 = 63 + 3 = 6 - Manhattan Distance is used -If there is a tie, consider the one with lowest heuristic first -If there is still a tie, consider in Up, Down, Left, Right order

65 4 + 4 = 85 + 3 = 8 3 + 3 = 64 + 2 = 6Dest 2 + 4 = 6 1 + 5 = 63 + 3 = 6 Start (0 + 6 = 6)1 + 5 = 62 + 4 = 63 + 3 = 64 + 4 = 8 - Manhattan Distance is used -If there is a tie, consider the one with lowest heuristic first -If there is still a tie, consider in Up, Down, Left, Right order

66 4 + 4 = 85 + 3 = 86 + 2 = 8 3 + 3 = 64 + 2 = 6Dest 2 + 4 = 6 1 + 5 = 63 + 3 = 6 Start (0 + 6 = 6)1 + 5 = 62 + 4 = 63 + 3 = 64 + 4 = 8 - Manhattan Distance is used -If there is a tie, consider the one with lowest heuristic first -If there is still a tie, consider in Up, Down, Left, Right order

67 4 + 4 = 85 + 3 = 86 + 2 = 87 + 1 = 8 3 + 3 = 64 + 2 = 6Dest 2 + 4 = 6 1 + 5 = 63 + 3 = 6 Start (0 + 6 = 6)1 + 5 = 62 + 4 = 63 + 3 = 64 + 4 = 8 - Manhattan Distance is used -If there is a tie, consider the one with lowest heuristic first -If there is still a tie, consider in Up, Down, Left, Right order

68 4 + 4 = 85 + 3 = 86 + 2 = 87 + 1 = 88 + 2 = 10 3 + 3 = 64 + 2 = 6Dest (8 + 0 = 8) 2 + 4 = 6 1 + 5 = 63 + 3 = 6 Start (0 + 6 = 6)1 + 5 = 62 + 4 = 63 + 3 = 64 + 4 = 8 - Manhattan Distance is used -If there is a tie, consider the one with lowest heuristic first -If there is still a tie, consider in Up, Down, Left, Right order

69 4 + 4 = 85 + 3 = 86 + 2 = 87 + 1 = 88 + 2 = 10 3 + 3 = 64 + 2 = 6Dest (8 + 0 = 8) 2 + 4 = 6 1 + 5 = 63 + 3 = 6 Start (0 + 6 = 6)1 + 5 = 62 + 4 = 63 + 3 = 64 + 4 = 8 - Manhattan Distance is used -If there is a tie, consider the one with lowest heuristic first -If there is still a tie, consider in Up, Down, Left, Right order

70 A* Search = Past + Future A* Search = Past + Future A* Search – Future = ? A* Search – Future = ? Dijkstra Dijkstra A* Search – Past = ? A* Search – Past = ? Greedy Greedy

71 How about graph with negative edges? How about graph with negative edges? – Bellman-Ford

72 Consider the source as weight 0, others as infinity Consider the source as weight 0, others as infinity Count = 0, Improve = true Count = 0, Improve = true While (Count < n and Improve) { While (Count < n and Improve) { – Improve = false – Count++ – For each edge uv If u.weight + uv distance < v.weight { If u.weight + uv distance < v.weight { – Improve = true – v.weight = u.weight + uv.distance } } If (Count == n) print “Negative weight cycle detected” If (Count == n) print “Negative weight cycle detected” Else print shortest path distance Else print shortest path distance

73 Time Compleity: O(VE) Time Compleity: O(VE)

74 All we have just dealt with is single source All we have just dealt with is single source How about multiple sources (all sources)? How about multiple sources (all sources)? – Floyd-Warshall Algorithm

75 Makes use of memoization Makes use of memoization 1. Find all smallest 1-hop distance 1. Find all smallest 1-hop distance 2. Find all smallest 2-hops distance 2. Find all smallest 2-hops distance 3. … 3. … N. Find all smallest n-hops distance N. Find all smallest n-hops distance Done ! Done !

76 For (int k = 0; k < MAX; k++) { For (int k = 0; k < MAX; k++) { – For (int I = 0; I < MAX; i++) { For (int j = 0; j < MAX; j++) { For (int j = 0; j < MAX; j++) { – Path[i][j] = min(Path[i][j], Path[i][k] + Path[k][j]); } – } }

77 What is iterative-deepening? What is iterative-deepening? Given limited time, find the current most optimal solution Given limited time, find the current most optimal solution Dijkstra (Shortest 1-step Solution) Dijkstra (Shortest 1-step Solution) Dijkstra (Shortest 2-steps Solution) Dijkstra (Shortest 2-steps Solution) Dijkstra (Shortest 3-steps Solution) Dijkstra (Shortest 3-steps Solution) … Dijkstra (Shortest n-steps Solution) Dijkstra (Shortest n-steps Solution) Suitable in bi-directional search (Faster than 1-way search) Suitable in bi-directional search (Faster than 1-way search)

78 s t

79 s t

80 Given an undirected graph, find the minimum cost so that every node has path to other nodes Given an undirected graph, find the minimum cost so that every node has path to other nodes Kruskal’s Algorithm Kruskal’s Algorithm Prim’s Algorithm Prim’s Algorithm

81 Continue Finding Shortest Edge Continue Finding Shortest Edge If no cycle is formed If no cycle is formed – add it into the list – Construct a parent-child relationship If all nodes have the same parent, we are done (path compression) If all nodes have the same parent, we are done (path compression)

82 NodeABCDE ParentABCDE A B C D E 4 3 1 2 7 8 10

83 A B C D E 4 3 1 2 7 8 Edge List: BC1 NodeABCDE ParentABBDE 10

84 A B C D E 4 3 1 2 7 8 Edge List: BC1 NodeABCDE ParentABBBE 10

85 A B C D E 4 3 1 2 7 8 Edge List: BC1 NodeABCDE ParentAAAAE 10

86 A B C D E 4 3 1 2 7 8 Edge List: BC1 NodeABCDE ParentAAAAE Cycle formed 10

87 A B C D E 4 3 1 2 7 8 Edge List: BC1 NodeABCDE ParentAAAAE 10

88 A B C D E 4 3 1 2 7 8 Edge List: BC1 NodeABCDE ParentAAAAE Cycle formed 10

89 A B C D E 4 3 1 2 7 8 Edge List: BC1 NodeABCDE ParentAAAAA 10

90 Path Compression can be done when we find parent Path Compression can be done when we find parent int find_parent(int x) { return (parent[x] == x)? x: return (parent[x] == x)? x: (parent[x] = find_parent(parent[x])); (parent[x] = find_parent(parent[x]));}

91 While not all nodes are visited While not all nodes are visited – While the list is not empty and the minimum edge connects to visited node Pop out the edge Pop out the edge – If the list is empty, print Impossible and return – Else Mark that node as visited Mark that node as visited Add all its unvisited edges to the list Add all its unvisited edges to the list Print the tree Print the tree

92 A B C D E 4 3 1 2 7 8 10 Edge List: AC3 AB4

93 A B C D E 4 3 1 2 7 8 10 Edge List: CB1 AB4 CD7 CE10

94 A B C D E 4 3 1 2 7 8 10 Edge List: BD2 AB4 CD7 CE10

95 A B C D E 4 3 1 2 7 8 10 Edge List: AB4 CD7 DE8 CE10

96 A B C D E 4 3 1 2 7 8 10 Edge List: CD7 DE8 CE10 Cycle formed

97 A B C D E 4 3 1 2 7 8 10 Edge List: DE8 CE10 Cycle formed

98 A B C D E 4 3 1 2 7 8 10 Edge List: CE10 We are done

99 We have just dealt with undirected MST. We have just dealt with undirected MST. How about directed? How about directed? Using Kruskal’s or Prim’s cannot solve directed MST problem Using Kruskal’s or Prim’s cannot solve directed MST problem How? How? Chu-Liu/Edmonds Algorithm Chu-Liu/Edmonds Algorithm

100 For each vertex, find the smallest incoming edge For each vertex, find the smallest incoming edge While there is cycle 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 Print out the tree

101 A B C D E 10 3 5 2 8 4 2 79

102 Cycle formed (B, C, D) A  B, C, D = 10 – 2 = 8 A  B, C, D = 3 – 2 = 1 E  B, C, D = 9 – 4 = 5 A B C D E 10 3 5 2 8 4 2 79

103 No Cycle formed, that’s it !!! A B C D E 10 3 5 2 8 4 2 79

104 Using this algorithm may unloop a cycle and create another cycle… 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 … But since the length of tree is increasing, there will have an end eventually Worst Case is doing n-1 times Worst Case is doing n-1 times Time Complexity is O(EV) Time Complexity is O(EV)

105 A graph with weight (capacity) A graph with weight (capacity) Mission:Find out the maximum flow from source to destination Mission:Find out the maximum flow from source to destination How? How?

106 Do { Do { – Do BFS on the graph to find maximum flow in the graph – Add it to the total flow – For each edge in the maximum flow – Deduct the flow just passed } While the flow is not zero; } While the flow is not zero;

107


Download ppt "Charles Lin. Graph Representation Graph Representation DFS DFS BFS BFS Dijkstra Dijkstra A* Search A* Search Bellman-Ford Bellman-Ford Floyd-Warshall."

Similar presentations


Ads by Google