1.91k likes | 1.92k Views
Explore graph data structures, edge types, terminologies, and properties. Learn about graph ADT implementation and various data structures. Understand adjacency list, map, and matrix structures effectively.
E N D
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
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
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
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
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
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
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
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
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
Graph ADT: part 1 Andy Mirzaian
Graph ADT: part 2 Andy Mirzaian
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
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
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
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
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
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
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
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
Performance Andy Mirzaian
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
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
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
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
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.
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.
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?
Graph Search s b a e d g c f j i h m k l
Graph Search s b a e d g c f j i h m k l
Graph Search s b a e d g c f j i h m k l
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
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.
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
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.
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
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.
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
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.
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.
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.
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)
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
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.
BFS FoundNot HandledQueue s b a e d g c f j i h m k l
BFS FoundNot HandledQueue d=0 s b a s d=0 e d g c f j i h m k l
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
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
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