Presentation is loading. Please wait.

Presentation is loading. Please wait.

Yan Shi CS/SE 2630 Lecture Notes

Similar presentations


Presentation on theme: "Yan Shi CS/SE 2630 Lecture Notes"— Presentation transcript:

1 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

2 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 )

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? N vertices: complete directed graph has N(N-1) edges; complete undirected graph has N(N-1)/2 edges.

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 Houston Dallas

26 Austin to Washington Denver Chicago Houston

27 Austin to Washington Atlanta Denver Chicago

28 Austin to Washington Atlanta Denver

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: 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

35 Dijkstra’s algorithm example
1 2 3 4 5 6 7 10 8

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

37 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

38 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

39 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

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

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


Download ppt "Yan Shi CS/SE 2630 Lecture Notes"

Similar presentations


Ads by Google