Yan Shi CS/SE 2630 Lecture Notes 14. Graphs Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++ Plus Data Structure textbook slides
What is a Graph? Tree: Graph: 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? N vertices: complete directed graph has N(N-1) edges; complete undirected graph has N(N-1)/2 edges.
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 Houston Dallas
Austin to Washington Denver Chicago Houston
Austin to Washington Atlanta Denver Chicago
Austin to Washington Atlanta Denver
Austin to Washington Atlanta
Austin to Washington Washington
Austin to 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 di and 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, Update all the vertices still in V-S connected to u to a better estimation if possible
Dijkstra’s algorithm example 1 2 3 4 5 6 7 10 8
Dijkstra’s algorithm example 0+7=7 1 2 3 4 5 6 7 10 8 0+10=10
Dijkstra’s algorithm example 0+7=7 7+10=17 1 2 3 4 5 6 7 10 8 0+10=10 7+6=13
Dijkstra’s algorithm example 0+7=7 7+10=17 1 2 3 4 5 6 7 10 8 0+10=10 7+6=13
Dijkstra’s algorithm example 0+7=7 7+6+1=14 7+6+8=21 1 2 3 4 5 6 7 10 8 0+10=10 7+6=13
Dijkstra’s algorithm example 0+7=7 7+6+1=14 7+6+1+2=16 1 2 3 4 5 6 7 10 8 0+10=10 7+6=13
Dijkstra’s algorithm example 0+7=7 7+6+1=14 7+6+1+2=16 1 2 3 4 5 6 7 10 8 0+10=10 7+6=13