1 / 41

14. Graphs

Learn about graphs, their types, implementations, and common algorithms like depth-first search, breadth-first search, and Dijkstra's algorithm.

jrodriguez
Télécharger la présentation

14. 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. 14. Graphs Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++ Plus Data Structure textbook slides

  2. What is a Graph? • Tree: • great for representing hierarchical structures. • each node has only one parent. • Graph: • generalization of a tree • a graph G is defined as G = (V,E) • V: a set of vertices (nodes) • E: a set of edges (connecting the vertices) • undirected graph: edges have no direction ( road map ) • directed graph: edges have directions ( flight routes )

  3. Undirected Graph Adjacent vertices:Two vertices in a graph that are connected by an edge e.g. A and B, B and C Path: A sequence of vertices that connects two nodes in a graph e.g. Path(A,C) = ?

  4. Directed Graph Root: a vertex with no incoming edge Do we have any root in this graph?

  5. Complete Graph • A graph in which every vertex is directly connected to every other vertex • How many edges will it have?

  6. Clique • a clique in an undirected graph is a subset of its vertices such that every two vertices in the subset are connected by an edge. • E.g., {A,B,D} is a 3-clique.

  7. Weighted Graph • A graph in which each edge carries a value

  8. How to Implement a Graph? • If the graph is quite complete, we can use a 2D array  adjacent matrix • If the graph is quite sparse, for each vertex, we can use a list to identify outgoing edges  adjacent list

  9. Adjacency Matrix Implementation • Adjacency Matrix:for a graph with N nodes, an N by N table that shows the existence (and weights) of all edges in the graph

  10. Adjacent List Implementation • Adjacency List:A linked list that identifies all the vertices to which a particular vertex is connected; each vertex has its own adjacency list

  11. Time Complexity of Add/Remove • Assume we have an adjacent list implementation • There are n vertices and m edges • Assume we know the exact location of the vertex/edge, What is the time complexity to • Add a vertex • Remove a vertex • Add an edge • Remove an edge O(1) O(m) O(1) O(1)

  12. Common Graph Algorithms • Depth-first search traversal algorithm: • Visit all the nodes in a branch to its deepest point before moving up • similar to post-order traversal of a tree • stack-based • Breadth-first search traversal algorithm: • Visit all the nodes on one level before going to the next level • similar to pre-order traversal of a tree • queue-based • Shortest-path algorithm: • An algorithm that displays the shortest path from a designated starting node to every other node in the graph

  13. Depth First Search • Question: can I get to vertex B from vertex A? • DFS Algorithm: go as far as possible first • Time Complexity: O( n + m ) found = false stack.push(A) visited = empty set while !found && !stack.empty() vertex = stack.pop() found = ( vertex == B ) if ( !found ) for each v adjacent to vertex and not marked mark v stack.push(v) if found write "path exists!"

  14. Austin to Chicago Austin

  15. Austin to Chicago Houston Dallas

  16. Austin to Chicago Atlanta Dallas

  17. Austin to Chicago Washington Dallas

  18. Austin to Chicago Dallas

  19. Austin to Chicago Denver Chicago

  20. Austin to Chicago Chicago

  21. Austin to Chicago Chicago Found

  22. Depth First Traversal from Austin Austin  Houston  Atlanta  Washington  Dallas  Denver  Chicago

  23. Breadth First Search • try all adjacent nodes first • Algorithm: found = false queue.enqueue(A) visited = empty set while !found && !queue.empty() vertex = queue.dequeue() found = ( vertex == B ) if ( !found ) for each v adjacent to vertex and not marked mark v queue.enqueue(v) if found write "path exists!"

  24. Austin to Washington Austin

  25. Austin to Washington Dallas Houston

  26. Austin to Washington Denver Houston Chicago

  27. Austin to Washington Atlanta Chicago Denver

  28. Austin to Washington Denver Atlanta

  29. Austin to Washington Atlanta

  30. Austin to Washington Washington

  31. Austin to Washington

  32. Breath First Traversal from Austin Austin  Dallas  Houston  Chicago  Denver  Atlanta  Washington

  33. Single Source Shortest Path • Given: Weighted directed graph G, single sources s. • Goal: Find shortest paths from s to every other vertex • Very similar to depth and breadth first search except: • use a minimum priority queue instead of a stack or queue • there is no destination: stop only when there are no more cities in the process

  34. Dijkstra’s algorithm • Dijkstra’s algorithm: http://en.wikipedia.org/wiki/Dijkstra's_algorithm • G = (V,E} • S = {vertices whose shortest paths from the source is determined} • di = best estimate of shortest path to vertex i • pi = predecessors • Initialize diand pi, • Set S to empty, • While there are still vertices in V-S, • Sort the vertices in V-S according to the current best estimate of their distance from the source, • Add u, the closest vertex in V-S, to S, • Updateall the vertices still in V-S connected to uto a better estimation if possible

  35. Dijkstra’salgorithm example 10 2 2 4 6 7 5 6 1 8 1 3 5 10 4 0

  36. Dijkstra’salgorithm example 10 2 2 4 6 0+7=7 7 5 6 1 8 1 3 5 10 4 0 0+10=10

  37. Dijkstra’salgorithm example 10 2 2 4 6 0+7=7 7+10=17 7 5 6 1 8 1 3 5 10 4 0 0+10=10 7+6=13

  38. Dijkstra’salgorithm example 10 2 2 4 6 0+7=7 7+10=17 7 5 6 1 8 1 3 5 10 4 0 0+10=10 7+6=13

  39. Dijkstra’salgorithm example 10 2 2 4 6 0+7=7 7+6+1=14 7+6+8=21 7 5 6 1 8 1 3 5 10 4 0 0+10=10 7+6=13

  40. Dijkstra’salgorithm example 10 2 2 4 6 0+7=7 7+6+1=14 7+6+1+2=16 7 5 6 1 8 1 3 5 10 4 0 0+10=10 7+6=13

  41. Dijkstra’salgorithm example 10 2 2 4 6 0+7=7 7+6+1=14 7+6+1+2=16 7 5 6 1 8 1 3 5 10 4 0 0+10=10 7+6=13

More Related