1 / 121

G64ADS Advanced Data Structures

G64ADS Advanced Data Structures. Graph Algorithms. 1. Going from A to B …. What data structure to use? How to think about the problem?. C. A. B. F. D. E. H. G. 2. Going from A to B …. Strip away the irrelevant details … Vertices. C. A. B. F. D. E. H. G. 3.

pbowen
Download Presentation

G64ADS Advanced Data Structures

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. G64ADSAdvanced Data Structures Graph Algorithms 1

  2. Going from A to B … • What data structure to use? • How to think about the problem? C A B F D E H G 2

  3. Going from A to B … Strip away the irrelevant details … Vertices C A B F D E H G 3

  4. Going from A to B … Strip away the irrelevant details … Edges C A B F D E H G 4

  5. Going from A to B … Strip away the irrelevant details … Weights 4 C 3 A B 3 F 4 4 D E 4 5 6 H G 5

  6. Going from A to B … Strip away the irrelevant details … Now, much simpler … 4 C 3 A B 3 F 4 4 D E 4 5 6 H G 6

  7. Graphs • Are a tool for modeling real world problems. • Allow us to abstract details and focus on the problem. • We can represent our domain as graphs apply algorithms to solve our problem.

  8. Simple Graphs

  9. Directed Graphs

  10. 4 3 3 4 4 4 5 6 Weighted Graphs

  11. Path and Cycle

  12. Representation of Graphs • Adjacency matrix • Adjacency list

  13. Representation of Graphs • Adjacency list

  14. Topological Sort • Ordering of vertices in a directed acyclic graph, such that if there is a path from vi to vj, the vj appears after vi in the ordering

  15. Topological Sort • Application • Given a number of tasks, there are often a number of constraints between the tasks: • task A must be completed before task B can start • These tasks together with the constraints form a directed acyclic graph • A topological sort of the graph gives an order in which the tasks can be scheduled while still satisfying the constraints

  16. Topological Sort • Application • Consider someone getting ready to go out for dinner • He must wear the following: • jacket, shirt, briefs, socks, tie, Blackberry, etc. • There are certain constraints: • the pants really should go on after the briefs, • the Blackberry must be clipped on the belt • socks are put on before shoes

  17. Topological Sort • Application • Getting dress graph

  18. Topological Sort • Application • Getting dress graph • One topological sort is: • briefs, pants, wallet, keys, belt, Blackberry, socks, shoes, shirt, tie, jacket, iPod, watch

  19. Topological Sort • Application • Getting dress graph another possible solution: briefs, socks, pants, shirt, belt, tie, jacket, wallet, keys, Blackberry, iPod, watch, shoes • One topological sort is: • briefs, pants, wallet, keys, belt, Blackberry, socks, shoes, shirt, tie, jacket, iPod, watch Not unique,

  20. Topological Sort • Course prerequisite structure represented in an acyclic graph

  21. Topological Sort • Topological Sort (Relabel): Given a directed acyclic graph (DAG), relabel the vertices such that every directed edge points from a lower-numbered vertex to a higher numbered one 6 1 5 8 3 2 7 4 3 5 2 7 8 4 1 6

  22. Topological Sort • Topological Sort (rearrange): Given a directed acyclic graph (DAG), rearrange its vertices on a horizontal line such that all the directed edges points from left to right. 6 1 5 8 3 2 7 4 7 5 6 2 1 4 8 3

  23. web.mit.edu/jorlin/www/15.082/Animations/Topological_Sorting.pptweb.mit.edu/jorlin/www/15.082/Animations/Topological_Sorting.ppt Following slides are taken from

  24. 1 2 3 4 5 6 7 8 Node Indegree 2 2 3 2 1 1 0 2 7 0 next LIST Initialization 6 1 “Next” will be the label of nodes in the topological order. Determine the indegree of each node LIST is the set of nodes with indegree of 0. 5 8 3 2 7 4

  25. LIST Select a node from LIST 6 1 next := next +1order(i) := next; update indegrees update LIST Select a node from LIST and delete it. 5 8 3 2 7 7 4 1 0 1 next 1 2 3 4 5 6 7 8 Node Indegree 2 2 3 1 2 0 1 1 0 2 7 5

  26. LIST Select a node from LIST 6 1 next := next +1order(i) := next; update indegrees update LIST Select a node from LIST and delete it. 2 5 5 8 3 2 7 7 4 1 2 0 1 next 1 2 3 4 5 6 7 8 Node Indegree 2 2 1 3 0 1 2 0 1 1 0 0 2 4 5 7 6

  27. LIST Select a node from LIST 3 6 6 1 next := next +1order(i) := next; update indegrees update LIST Select a node from LIST and delete it. 2 5 5 8 3 2 7 7 4 1 3 1 2 0 next 1 2 3 4 5 6 7 8 Node Indegree 1 2 2 1 0 3 2 1 0 0 1 0 1 0 2 4 7 5 6 2

  28. LIST Select a node from LIST 3 6 6 1 next := next +1order(i) := next; update indegrees update LIST Select a node from LIST and delete it. 2 5 5 2 2 8 3 4 7 7 4 1 4 1 3 2 0 next 1 2 3 4 5 6 7 8 Node Indegree 0 1 2 1 0 2 3 0 1 2 1 0 1 0 0 2 4 5 7 2 6 1

  29. LIST Select a node from LIST 5 3 6 6 1 1 next := next +1order(i) := next; update indegrees update LIST Select a node from LIST and delete it. 2 5 5 2 2 8 3 4 7 7 4 1 1 2 5 0 4 3 next 1 2 3 4 5 6 7 8 Node Indegree 2 1 0 2 0 1 2 3 2 1 0 0 1 0 1 0 2 1 4 7 5 1 2 6

  30. LIST Select a node from LIST 5 3 6 6 1 1 next := next +1order(i) := next; update indegrees update LIST Select a node from LIST and delete it. 2 5 5 2 2 8 3 4 7 7 4 4 1 6 1 0 2 4 5 3 6 next 1 2 3 4 5 6 7 8 Node Indegree 1 0 2 0 1 2 3 2 1 2 0 1 0 1 1 0 0 1 0 2 6 4 1 2 5 7 8

  31. LIST Select a node from LIST 5 3 6 6 1 1 next := next +1order(i) := next; update indegrees update LIST Select a node from LIST and delete it. 7 2 5 5 2 2 8 8 3 4 7 7 4 4 1 6 1 4 5 6 2 3 7 0 next 1 2 3 4 5 6 7 8 Node Indegree 2 1 0 0 1 2 0 3 2 1 0 1 2 0 1 0 1 0 2 1 0 8 3

  32. LIST Select a node from LIST 5 3 6 6 1 1 next := next +1order(i) := next; update indegrees update LIST Select a node from LIST and delete it. 7 8 2 5 5 2 2 8 8 3 3 4 7 7 4 4 1 6 4 1 6 7 8 5 3 2 0 next List is empty. The algorithm terminates with a topological order of the nodes 1 2 3 4 5 6 7 8 Node Indegree 2 0 1 0 1 2 0 2 3 1 2 0 1 0 1 0 1 0 2 0 1 3

  33. Example • Consider the following DAG with six vertices

  34. Example • Let us define the table of in-degrees

  35. Example • And a queue into which we can insert vertices 1 and 6 Queue

  36. Example • We dequeue the head (1), decrement the in-degree of all adjacent vertices, and enqueue 2 Queue Sort 1

  37. Example • We dequeue 6 and decrement the in-degree of all adjacent vertices Queue Sort 1, 6

  38. Example • We dequeue 2, decrement, and enqueue vertex 5 Queue Sort 1, 6, 2

  39. Example • We dequeue 5, decrement, and enqueue vertex 3 Queue Sort 1, 6, 2, 5

  40. Example • We dequeue 3, decrement 4, and add 4 to the queue Queue Sort 1, 6, 2, 5, 3

  41. Example • We dequeue 4, there are no adjacent vertices to decrement the in degree Queue Sort 1, 6, 2, 5, 3, 4

  42. Example • The queue is now empty, so a topological sort is 1, 6, 2, 5, 3, 4

  43. Topological Sort

  44. Topological Sort

  45. Shortest-Path Algorithm • Find the “shortest” path from point A to point B • 􀂄“Shortest” in time, distance, cost, … • 􀂄Numerous applications • Map navigation • Flight itineraries • Circuit wiring • Network routing

  46. Shortest Path Problems • Input is a weighted graph where each edge (vi,vj) has cost ci,j to traverse the edge • 􀂄Cost of a path v1v2…vN • Unweighted path length is N-1, number of edges on path

  47. Shortest Path Problems • Single-source shortest path problem • Given a weighted graph G=(V,E), and a start vertex s, find the minimum weighted path from s to every other vertex in G

  48. Negative Weights • Graphs can have negative weights • e.g., arbitrage • Shortest positive-weight path is a net gain • Path may include individual losses • Problem: Negative weight cycles • Allow arbitrarily-low path costs • Solution • Detect presence of negative-weight cycles

  49. Shortest Path Problems • Unweighted shortest-path problem: O(|E|+|V|) • Weighted shortest-path problem • No negative edges: O(|E| log |V|) • Negative edges: O(|E|·|V|) • Acyclic graphs: O(|E|+|V|) • No asymptotically faster algorithm for single-source/single-destination shortest path problem

  50. Unweighted Shortest Paths • No weights on edges • Find shortest length paths • Same as weighted shortest path with all weights equal • Breadth-first search

More Related