1 / 191

Graph ADT and Algorithmic Paradigms

Graph ADT and Algorithmic Paradigms. Graph ADT Generic Search Breadth First Search Dijkstra's Shortest Paths Algorithm Depth First Search Linear Order. Jeff Edmonds York University. COSC 2011. Lecture 8. Graphs. A graph is a pair ( V, E ), where V is a set of nodes, called vertices

henryv
Télécharger la présentation

Graph ADT and Algorithmic Paradigms

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. Graph ADTand Algorithmic Paradigms Graph ADT Generic Search Breadth First Search Dijkstra's Shortest Paths Algorithm Depth First Search Linear Order Jeff Edmonds York University COSC 2011 Lecture8

  2. Graphs • A graph is a pair (V, E), where • V is a set of nodes, called vertices • E is a collection of pairs of vertices, called edges • Vertices and edges are positions and store elements • Example: • A vertex represents an airport and stores the three-letter airport code • An edge represents a flight route between two airports and stores the mileage of the route 849 PVD 1843 ORD 142 SFO 802 LGA 1743 337 1387 HNL 2555 1099 1233 LAX 1120 DFW MIA Andy Mirzaian

  3. Edge Types • Directed edge • ordered pair of vertices (u,v) • first vertex u is the origin • second vertex v is the destination • e.g., a flight • Undirected edge • unordered pair of vertices (u,v) • e.g., a flight route • Directed graph • all the edges are directed • e.g., route network • Undirected graph • all the edges are undirected • e.g., flight network flight AA 1206 ORD PVD 849 miles ORD PVD Andy Mirzaian

  4. Applications • Electronic circuits • Printed circuit board • Integrated circuit • Transportation networks • Highway network • Flight network • Computer networks • Local area network • Internet • Web • Databases • Entity-relationship diagram Andy Mirzaian

  5. V a b h j U d X Z c e i W g f Y Terminology • End vertices (or endpoints) of an edge • U and V are the endpoints of a • Edges incident on a vertex • a, d, and b are incident on V • Adjacent vertices • U and V are adjacent • Degree of a vertex • X has degree 5 • Paralleledges • h and i are parallel edges • Self-loop • j is a self-loop Andy Mirzaian

  6. Terminology (cont.) • Path • sequence of alternating vertices and edges • begins with a vertex • ends with a vertex • each edge is preceded and followedby its endpoints • Simple path • path such that all its vertices and edges are distinct • Examples: • P1 = (V,b,X,h,Z) is a simple path • P2 = (U,c,W,e,X,g,Y,f,W,d,V) is a path that is not simple V b a P1 d U X Z P2 h c e W g f Y Andy Mirzaian

  7. Terminology (cont.) • Cycle • circular sequence of alternating vertices and edges • each edge is preceded and followed by its endpoints • Simple cycle • cycle such that all its vertices and edges are distinct • Examples: • C1 = (V,b,X,g,Y,f,W,c,U,a,) is a simple cycle • C2 = (U,c,W,e,X,g,Y,f,W,d,V,a,) is a cycle that is not simple V a b d U X Z C2 h e C1 c W g f Y Andy Mirzaian

  8. Properties Notation n number of vertices m number of edges deg(v)degree of vertex v Property 1 v deg(v)= 2m Proof:each edge is counted twice Property 2 In an undirected graph with no self-loops and no multiple edges m  n (n -1)/2 Proof:each vertex has degree at most (n -1) What is the bound for a directed graph? Andy Mirzaian

  9. Vertices and Edges • A graph is a collection of vertices and edges. • We model the abstraction as a combination of three data types: Vertex, Edge, and Graph. • A Vertex is a lightweight object that stores an arbitrary element provided by the user (e.g., an airport code) • We assume it supports a method, element(), to retrieve the stored element. • An Edge stores an associated object (e.g., a flight number, travel distance, cost), retrieved with the element( ) method. Andy Mirzaian

  10. Graph ADT: part 1 Andy Mirzaian

  11. Graph ADT: part 2 Andy Mirzaian

  12. Data Structures Implementations • Graphs: u v • Adjacency Matrix Column for each vertex Pointers between vertices w Row for each vertex Need quick assess to vertices Need to list edges for each vertex Entry for each edge

  13. Data Structures Implementations • Graphs: • Adjacency Matrix Edge List For each vertex Column for each vertex Vertex List Edge List Row for each vertex Entry for each edge

  14. Data Structures Implementations • Graphs: • Adjacency Matrix Edge List For each vertex Column for each vertex Vertex List Edge List Row for each vertex Entry for each edge

  15. Edge List Structure • Vertex object • element • reference to position in vertex sequence • Edge object • element • origin vertex object • destination vertex object • reference to position in edge sequence • Vertex sequence • sequence of vertex objects • Edge sequence • sequence of edge objects Andy Mirzaian

  16. Adjacency List Structure • Incidence sequence for each vertex • sequence of references to edge objects of incident edges • Augmented edge objects • references to associated positions in incidence sequences of end vertices Andy Mirzaian

  17. Adjacency Map Structure • Incidence sequence for each vertex • sequence of references to adjacent vertices, each mapped to edge object of the incident edge • Augmented edge objects • references to associated positions in incidence sequences of end vertices Andy Mirzaian

  18. Adjacency Matrix Structure • Edge list structure • Augmented vertex objects • Integer key (index) associated with vertex • 2D-array adjacency array • Reference to edge object for adjacent vertices • Null for non-adjacent vertices • The “old fashioned”version just has0 for no edge and 1 for edge Andy Mirzaian

  19. Adjacency Matrix Structure • Edge list structure • Augmented vertex objects • Integer key (index) associated with vertex • 2D-array adjacency array • Reference to edge object for adjacent vertices • Null for non-adjacent vertices • The “old fashioned”version just has0 for no edge and 1 for edge Andy Mirzaian

  20. Performance Andy Mirzaian

  21. Subgraphs • A subgraph S of a graph G is a graph such that • The vertices of S are a subset of the vertices of G • The edges of S are a subset of the edges of G • A spanning subgraph of G is a subgraph that contains all the vertices of G Subgraph Spanning subgraph Andy Mirzaian

  22. Connectivity • A graph is connected if there is a path between every pair of vertices • A connected component of a graph G is a maximal connected subgraph of G Connected graph Non connected graph with two connected components Andy Mirzaian

  23. Trees and Forests • A (free) tree is an undirected graph T such that • T is connected • T has no cycles This definition of tree is different from the one of a rooted tree • A forest is an undirected graph without cycles • The connected components of a forest are trees Tree Forest Andy Mirzaian

  24. Spanning Trees and Forests • A spanning tree of a connected graph is a spanning subgraph that is a tree • A spanning tree is not unique unless the graph is a tree • Spanning trees have applications to the design of communication networks • A spanning forest of a graph is a spanning subgraph that is a forest Graph Spanning tree Andy Mirzaian

  25. Graph Search

  26. Graph Search Specification: Reachability-from-single-source s • <preCond>: The input is a graph G(either directed or undirected) and a source node s. • <postCond>: Output all the nodes u that are reachable by a path in G from s.

  27. Graph Search v & there is an edge from u to v Basic Steps: s u Suppose you know that u is reachable from s You know that v is reachable from s Build up a set of reachable nodes.

  28. b t Graph Search reachable d b v e t w h e d d reachable h v f d w u j reachable reachable s • How do we keep track of all of this information? • How do we avoid cycling?

  29. Graph Search s b a e d g c f j i h m k l

  30. Graph Search s b a e d g c f j i h m k l

  31. Graph Search s b a e d g c f j i h m k l

  32. Graph Search s We know found nodes are reachable from s because we have traced out a path. b a e d g c f If a node has been handled, then all of its neighbors have been found. j i h m k l l

  33. Graph Search Handle some foundNotHandled node s We know found nodes are reachable from s because we have traced out a path. b a e d g c f If a node has been handled, then all of its neighbors have been found. j i h m k l i.e. find its neighbors Don’t re-find a node.

  34. Graph Search s We know found nodes are reachable from s because we have traced out a path. b a e d g c f If a node has been handled, then all of its neighbors have been found. j i h m k l Handle some foundNotHandled node i.e. find its neighbors

  35. Graph Search measureprogress s We know found nodes are reachable from s because we have traced out a path. b a e d g c f If a node has been handled, then all of its neighbors have been found. j i h m k l # of found nodes. Might not increase. # of handled nodes.

  36. Graph Search s We know found nodes are reachable from s because we have traced out a path. b a e d g c f If a node has been handled, then all of its neighbors have been found. j i h m k l Node s is foundNotHandled Other nodes notFound

  37. Graph Search Exit Handle some foundNotHandled node s We know found nodes are reachable from s because we have traced out a path. b a e d g c f If a node has been handled, then all of its neighbors have been found. j i h m k l All nodes found. No. Might not find all. When can’t make any more progress. When all found nodes are have been handled.

  38. Graph Search We know found nodes are reachable from s because we have traced out a path. If a node has been handled, then all of its neighbors have been found. Exit Exit All found nodes are handled. b a e d g c f j i h m k l • <postCond>: Output all the nodes u that are reachable by a path in G from s. Output Found nodes

  39. Graph Search Exit Exit We know found nodes are reachable from s because we have traced out a path. b a e d g c f If a node has been handled, then all of its neighbors have been found. j i h m k l All found nodes are handled. • <postCond>: Found nodes are reachable from s. Reachable nodes have been found.

  40. Graph Search Found = handled Exit handled Exit notfound We know found nodes are reachable from s because we have traced out a path. b a e d g c f If a node has been handled, then all of its neighbors have been found. j i h m k l All found nodes are handled. • <postCond>: Reachable nodes have been Found. [A  B] = [B A] notFound nodes not reachable.

  41. Graph Search Define Problem Define Loop Invariants Define Measure of Progress 79 km to school Define Step Define Exit Condition Maintain Loop Inv Exit 0 km Exit Exit Exit Exit 79 km 75 km Make Progress Initial Conditions Ending Specification of Reachability-from-single-source s • <preCond>: The input is a graph G(either directed or undirected) and a source node s. • <postCond>: Output all the nodes u that are reachable by a path in G from s.

  42. Graph Search O(n) iterations, but iteration takes more than O(1) f u j Handle some foundNotHandled node # of handled nodes. Time = O(n) O(n) neighbors = O(n2) Could be fewer? Each edge visited, times. 2 = O(E) Linear time. Size = O(E)

  43. Graph Search Which foundNotHandled node do we handle? • Queue: • Handle node • Found longest ago • Likely closest to s (in # of edges). • Breadth-First Search • Priority Queue: • Handle node that seems to be closest to s (weighted). • Dijkstra's Shortest-Weighted Paths • Stack: • Handle node • Found most recently • Likely farthest from s. • Depth-First Search

  44. Graph Search Which foundNotHandled node do we handle? • Queue: • Handle node • Found longest ago • Likely closest to s (in # of edges). • Breadth-First Search So far, the nodes have been found in order of length from s.

  45. BFS FoundNot HandledQueue s b a e d g c f j i h m k l

  46. BFS FoundNot HandledQueue d=0 s b a s d=0 e d g c f j i h m k l

  47. BFS FoundNot HandledQueue d=0 s d=1 b a d=0 a e d=1 d d g g c f b j i h m k l

  48. BFS FoundNot HandledQueue d=0 s d=1 b a a d=1 d e d g g b c f j i h m k l

  49. BFS FoundNot HandledQueue d=0 s d=1 b a d=1 d e d g g b c f c d=2 j f d=2 i h m k l

More Related