410 likes | 415 Views
Learn about graphs, their types, implementations, and common algorithms like depth-first search, breadth-first search, and Dijkstra's algorithm.
E N D
14. Graphs Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++ Plus Data Structure textbook slides
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 )
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) = ?
Directed Graph Root: a vertex with no incoming edge Do we have any root in this graph?
Complete Graph • A graph in which every vertex is directly connected to every other vertex • How many edges will it have?
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.
Weighted Graph • A graph in which each edge carries a value
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
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
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
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)
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
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!"
Austin to Chicago Austin
Austin to Chicago Houston Dallas
Austin to Chicago Atlanta Dallas
Austin to Chicago Washington Dallas
Austin to Chicago Dallas
Austin to Chicago Denver Chicago
Austin to Chicago Chicago
Austin to Chicago Chicago Found
Depth First Traversal from Austin Austin Houston Atlanta Washington Dallas Denver Chicago
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!"
Austin to Washington Austin
Austin to Washington Dallas Houston
Austin to Washington Denver Houston Chicago
Austin to Washington Atlanta Chicago Denver
Austin to Washington Denver Atlanta
Austin to Washington Atlanta
Austin to Washington Washington
Breath First Traversal from Austin Austin Dallas Houston Chicago Denver Atlanta Washington
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
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
Dijkstra’salgorithm example 10 2 2 4 6 7 5 6 1 8 1 3 5 10 4 0
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
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
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
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
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
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