1 / 85

Graphs

Graphs. David Kauchak cs302 Spring 2012. DAGs. Can represent dependency graphs. socks. underwear. shoes. pants. shirt. belt. watch. tie. jacket. Topological sort. A linear ordering of all the vertices such that for all edges (u,v)  E, u appears before v in the ordering

malbert
Download Presentation

Graphs

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. Graphs David Kauchak cs302 Spring 2012

  2. DAGs Can represent dependency graphs socks underwear shoes pants shirt belt watch tie jacket

  3. Topological sort • A linear ordering of all the vertices such that for all edges (u,v)  E, u appears before v in the ordering • An ordering of the nodes that “obeys” the dependencies, i.e. an activity can’t happen until it’s dependent activities have happened watch underwear pants socks shirt underwear belt shoes tie pants shirt socks belt watch tie shoes jacket jacket

  4. Topological sort

  5. Topological sort socks underwear shoes pants shirt belt watch tie jacket

  6. Topological sort socks underwear shoes pants shirt belt watch tie jacket

  7. Topological sort underwear socks shoes pants shirt belt watch tie jacket

  8. Topological sort underwear socks shoes pants shirt belt watch tie jacket

  9. Topological sort underwear pants socks shoes shirt belt watch tie jacket

  10. Topological sort underwear pants socks shoes shirt belt watch tie jacket

  11. Topological sort underwear pants shirt socks shoes belt watch tie jacket

  12. Topological sort underwear pants shirt … socks shoes belt watch tie jacket

  13. Running time?

  14. Running time? O(|V|+|E|)

  15. Running time? O(E) overall

  16. Running time? |V| How many calls?

  17. Running time? Overall running time? O(|V|2+|V| |E|)

  18. Can we do better?

  19. Topological sort 2

  20. Topological sort 2

  21. Topological sort 2

  22. Topological sort 2

  23. Running time? • How many times do we process each node? • How many times do we process each edge? • O(|V| + |E|)

  24. Connectedness Given an undirected graph, for every node u  V, can we reach all other nodes in the graph? Run BFS or DFS-Visit (one pass) and mark nodes as we visit them. If we visit all nodes, return true, otherwise false. Running time: O(|V| + |E|)

  25. Strongly connected Given a directed graph, can we reach any node v from any other node u? Ideas?

  26. A B C E D A B C E D Transpose of a graph • Given a graph G, we can calculate the transpose of a graph GR by reversing the direction of all the edges GR G Running time to calculate GR? O(|V| + |E|)

  27. Strongly connected

  28. s u t Is it correct? • What do we know after the first pass? • Starting at u, we can reach every node • What do we know after the second pass? • All nodes can reach u. Why? • We can get from u to every node in GR, therefore, if we reverse the edges (i.e. G), then we have a path from every node to u • Which means that any node can reach any other node. Given any two nodes s and t we can create a path through u … …

  29. Runtime? O(|V| + |E|) O(|V|) O(|V| + |E|) O(|V| + |E|) O(|V|) O(|V| + |E|)

  30. A B D Detecting cycles • Undirected graph • BFS or DFS. If we reach a node we’ve seen already, then we’ve found a cycle • Directed graph have to be careful

  31. Detecting cycles • Undirected graph • BFS or DFS. If we reach a node we’ve seen already, then we’ve found a cycle • Directed graph • Call TopologicalSort • If the length of the list returned ≠ |V| then a cycle exists

  32. A B C E D Shortest paths • What is the shortest path from a to d?

  33. A B C E D Shortest paths • BFS

  34. A B C E D Shortest paths • What is the shortest path from a to d? 2 3 3 2 1 1 4

  35. A B C E D Shortest paths • We can still use BFS 2 3 3 2 1 1 4

  36. D A B D E E B A C C Shortest paths • We can still use BFS 2 3 3 2 1 1 4

  37. A B C E D Shortest paths • We can still use BFS

  38. A B C E D Shortest paths • What is the problem?

  39. A B C A B C Shortest paths • Running time is dependent on the weights 100 2 50 1 200 4

  40. A B C A B C Shortest paths 100 50 200

  41. A B C Shortest paths

  42. A B C Shortest paths

  43. A B C Shortest paths Nothing will change as we expand the frontier until we’ve gone out 100 levels

  44. Dijkstra’s algorithm

  45. Dijkstra’s algorithm

  46. Dijkstra’s algorithm prev keeps track of the shortest path

  47. Dijkstra’s algorithm

  48. Dijkstra’s algorithm

  49. Dijkstra’s algorithm

  50. Single source shortest paths • All of the shortest path algorithms we’ll look at today are call “single source shortest paths” algorithms • Why?

More Related