1 / 46

Graphs

Graphs. Chapter 14. Simple Graph. a pair (V,E) V finite set of vertices points V = {a,b,c,d,e} E irreflexive, symmetric relation on V edges between vertices {{a,b},{b,c},{c,d},{d,e},{a,e}}. Directed graph. a pair (V,E) V finite set of vertices (points) E is an relation on V

nellie
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 Chapter 14

  2. Simple Graph • a pair (V,E) • V • finite set of vertices • points • V = {a,b,c,d,e} • E • irreflexive, symmetric relation on V • edges between vertices • {{a,b},{b,c},{c,d},{d,e},{a,e}}

  3. Directed graph • a pair (V,E) • V • finite set of vertices (points) • E is an relation on V • For any pair u,v in V, • E may contain one edge (u,v) from u to v, and (v,u) another edge from v to u. • see p. 432

  4. Adjacent, Incident • Vertices u and v in undirected graph G are called adjacent if {u,v} is an edge of G • If edge e = {u,v}, e is called incidentwith vertices u and v • Edge econnectsu and v

  5. Directed Edges • A directed edge (u,v) • shows a path from point u to point v • includes an arrow • u is called the initial vertex of (u,v) • v is called the terminal vertex of (u,v)

  6. Abstract Graph Graph Vertices Edges Create an empty graph Insert a vertex Insert an edge Delete a vertex Delete an edge Determine if graph is empty Determine all vertices adjacent to a given vertex

  7. Graph Representations

  8. Adjacency List • A graph representation • which specifies all vertices that are adjacent • to each vertex of the graph See digraph.h & Figure14-1.cpp

  9. Example Digraph A B D E C F G

  10. B F D F A E G F C G B Adjacency List Example A B C D E

  11. Adjacency List

  12. Adjacency Matrix AG • A graph representation • A n´n zero-one matrix • A = [aij] See graph.h & graphtst.cpp

  13. Example Digraph A B D E C F G

  14. Adjacency Matrix Example A B C D E F G A B C D E F G

  15. Depth First Search

  16. Searching a graph

  17. Depth-First Search

  18. Depth-First Search

  19. Depth-First Search http://www.cs.bme.hu/~gsala/alg_anims/3/graph-e.html

  20. Perform DFS from A A B D E C F G

  21. Breadth First Search

  22. Breadth-First Search http://www.cs.bme.hu/~gsala/alg_anims/3/graph2-e.html

  23. Breadth-First Search

  24. Breadth-First Search

  25. Perform BFS from A A B D E C F G

  26. Shortest Path

  27. Shortest Path Algorithms • Algorithms exist • to find the shortest path • between two vertices (Dijkstra) • http://www.dgp.toronto.edu/people/JamesStewart/270/9798s/Laffra/DijkstraApplet.html

  28. Shortest Unweighted Path • Performs a breadth-first processing of the nodes, assigning each of them a cost which is the length of the path from the starting node to the node • Uses a Queue of vertices to be examined • Picking a vertex is O(|V|)

  29. 1 function Dijkstra(Graph, source): 2 for each vertex v in Graph: // Initializations 3 dist[v] := infinity // Unknown distance function from source to v 4 previous[v] := undefined // Previous node in optimal path from source 5 dist[source] := 0 // Distance from source to source 6 Q := the set of all nodes in Graph// All nodes in the graph are unoptimized - thus are in Q 7 whileQis not empty: // The main loop 8 u := node in Q with smallest dist[] 9 remove u from Q 10 for each neighbor v of u: // where v has not yet been removed from Q. 11 alt := dist[u] + dist_between(u, v) // be careful in 1st step - dist[u] is infinity yet 12 ifalt < dist[v] // Relax (u,v) 13 dist[v] := alt 14 previous[v] := u 15 return previous[]

  30. Topological Ordering

  31. Topological Ordering • In a directed acyclic graph, • a topological ordering • is an ordering of vertices such that • if there is a path from u to v, • then v appears after u in the ordering • Example use – • prerequisite structure of classes

  32. Topological Ordering There are many problems where we can easily say that one task has to be done before another, but it is harder to work out what order to do a whole bunch of such things. Directed edges occur when one node depends on the other, because of prerequisite relationships among courses or dependencies among nodes. The problem in both is to find an acceptable ordering of the nodes satisfying the dependencies. This is referred to as a topological ordering.

  33. Topological Order - 1 • Start at CSI CSI CSII CSIII CS2813 CS4833 Discrete Math Analysis Algo. CSI

  34. Topological Order - 2 • Remove CSI, now can take CSII or CS 2813 CSII CSIII CS2813 CS4833 CSI, CSII, CS 2813 or CSI, CS2813, CSII

  35. Topological Order - 3 • Remove CSII, now take CS 2813 or CSIII CSIII CS2813 CS4833 CSI, CSII, CS 2813 or CSI, CSII, CSIII

  36. Topological Order - 4 • Remove CSIII, now can take CS2813 CS2813 CS4833 CSI, CSII, CSIII, CS 2813

  37. Topological Order - 5 • Remove CS 2813, now take CS 4833 CS4833 CSI, CSII, CSIII, CS 2813, CS 4833

  38. Several Topological Orderings CSI CSII CSIII CS2813 CS4833 CSI, CS 2813, CSII, CS 4833, CSIII or CS I, CS 2813, CSII, CSIII, CS 4833 or CSI, CSII, CS 2813, CS 4833, CSIII etc.. . . . . . .

  39. Topological Ordering The same problem arises in spreadsheets. Cell Contents Value 1 100 100 2 (Cell 1) + 10 110 3 (Cell 1) * 2 200 4 (Cell 2) + (Cell 3) 310 In the above example, if you change the value in cell 1, you need to first recalculate the values in cell 2 and 3, and only when that is done can you recalculate the value in cell 4.

  40. 1 2 4 3 Cell Contents Value 1 100 100 2 (Cell 1) + 10 110 3 (Cell 1) * 2 200 4 (Cell 2) + (Cell 3) 310

  41. Topological Sorting • A topological sort finds a topological ordering • Algorithm: • Define the indegree of a vertex, v, as the number of edges (u,v) in the graph • If the graph is acyclic, there must be at least one vertex with indegree of 0

  42. Topological Sorting Algo. • Take all vertices with indegree 0 and put on a queue • Remove the first vertex from the queue, “remove” it from the graph, and output • Put any new vertices with degree zero on the queue • Repeat last two steps until all vertices have been visited

  43. Topo Sort Algorithm L ← Empty list that will contain the sorted elements S ← Set of all nodes with no incoming edges while S is non-empty do remove a node n from S insert n into L for each node m with an edge e from n to m do remove edge e from the graph if m has no other incoming edges then insert m into S if graph has edges then output error message (graph has at least one cycle) else output message (proposed topologically sorted order: L)

  44. 1 2 4 3 Cell Contents Value 1 100 100 2 (Cell 1) + 10 110 3 (Cell 1) * 2 200 4 (Cell 2) + (Cell 3) 310 Topological Ordering: 1 -> 2 -> 3 -> 4 OE 1 -> 3 -> 2 -> 4

  45. Shortest Path

More Related