Presentation is loading. Please wait.

Presentation is loading. Please wait.

14 Graph ADTs  Graph concepts.  Graph applications.  A graph ADT: requirements, contract.  Implementations of graphs: edge-set, adjacency-set, adjacency-matrix.

Similar presentations


Presentation on theme: "14 Graph ADTs  Graph concepts.  Graph applications.  A graph ADT: requirements, contract.  Implementations of graphs: edge-set, adjacency-set, adjacency-matrix."— Presentation transcript:

1 14 Graph ADTs  Graph concepts.  Graph applications.  A graph ADT: requirements, contract.  Implementations of graphs: edge-set, adjacency-set, adjacency-matrix representations.  Graph algorithms: traversal, search, shortest distance, shortest path. © 2008 David A Watt, University of Glasgow Algorithms & Data Structures (M)

2 14-2 Graph concepts (1)  A graph consists of vertices connected by edges in an arbitrary manner.  Each vertex (or node) contains a single element.  Each edge connects two vertices together.  Each edge may optionally contain an edge attribute.  The size of a graph is the number of vertices.  The empty graph has no vertices (and therefore no edges).

3 14-3 Graph concepts (2)  In an undirected graph, edges have no direction:  In a directed graph, each edge has a particular direction: W V X TU W S W V X TU W S undirected edges vertices directed edges

4 14-4 W V X TU W S Graph concepts (3)  A path in a graph is a list of vertices, such that each pair of consecutive vertices is connected by an edge in the graph.  In a directed graph, a path must respect the directions of the edges. «S, T, W, V » is a path.«S, T, U, V » is a path. «S, T, W, V» is not a path. W V X TU W S

5 14-5 Graph applications  A road network is an undirected graph in which each vertex is a place and each edge is a road connecting two places. (The edge attributes might be road numbers or distances.)  A computer network is an undirected graph in which each vertex is a computer and each edge is a point-to-point connection.  The World-Wide Web is a directed graph in which each vertex is a document and each edge is a hyperlink.

6 14-6 Example: road network  Simplified road network for GB and Ireland: 140 160 170 150 190 130 140 6070100 150 220 160 190 120 110 120 70 60 London Edinburgh Glasgow Carlisle Manchester Birmingham Bristol Exeter Swansea Liverpool Leeds Rugby Hull Cambridge Dover Southampton Newcastle Dublin Belfast «Edinburgh, Newcastle, Leeds» is not a path. «Dover, London, Bristol, Exeter» is a path.

7 14-7 Graph concepts (4)  A cycle in a graph is a path whose first and last vertices are the same.  A cyclic graph is one that contains at least one cycle. An acyclic graph is one that contains no cycle. This graph is cyclic, since «T, U, V, W, T» is a cycle. W V X TU W S This graph is acyclic. D C A B

8 14-8 Graph terminology (1)  Two vertices of a graph are neighbours if they are connected by an edge.  The degree of a vertex is the number of edges connecting it with other vertices. U’s neighbours are T, V, X. U’s degree is 3. W V X TU W S

9 14-9 Graph terminology (2)  In a directed graph, if e is an edge connecting vertex v to vertex w: –v is the source of e, and w is the destination of e. –e is an in-edge of w, and an out-edge of v. –w is a successor of v, and v is a predecessor of w.  In a directed graph: –the in-degree of a vertex is its number of in-edges; –the out-degree of a vertex is its number of out-edges. U’s in-degree is 1. Its out-degree is 2. e vw W V X TU W S

10 14-10 Graph ADT: requirements (1)  Requirements (for both undirected and directed graphs): 1)It must be possible to make a graph empty. 2)It must be possible to add a new vertex, or to add a new edge connecting two existing vertices. 3)It must be possible to remove a vertex (and all of its connecting edges), or to remove a given edge. 4)It must be possible to test whether a graph has an edge connecting two given vertices. 5)It must be possible to access all vertices or all edges. 6)It must be possible to access all neighbours, or all connecting edges, of a given vertex.

11 14-11 Graph ADT: requirements (2)  Requirements (continued): 7)It must be possible to inspect or update the element contained in a given vertex. 8)It must be possible to inspect or update the attribute (if any) contained in a given edge.  Additional requirement for directed graphs only: 9)It must be possible to access all successors, or all out- edges, of a given vertex in a directed graph.  Note: Vertices and edges must be visible outside the graph ADT.

12 14-12 Graph ADT: contract (1)  Possible contract for all graphs: public interface Graph { // Each Graph object is a (directed or // undirected) graph whose elements are of type E // and whose edge attributes are of type A.

13 14-13 Graph ADT: contract (2)  Possible contract (continued): //////////// Accessors //////////// public int size (); // Return the number of vertices in this graph. public int degree (Vertex v); // Return the number of edges connecting vertex v in // this graph. public boolean containsEdge ( Vertex v0, Vertex v1); // Return true if and only if these is an edge // connecting vertices v0 and v1 in this graph. (If the // graph is directed, v0 is the edge’s source and v1 is // its destination.)

14 14-14 Graph ADT: contract (3)  Possible contract (continued): //////////// Transformers //////////// public void clear (); // Make this graph empty. public Vertex addVertex (E elem); // Add to this graph a new vertex containing elem // but with no connecting edges, and return the new // vertex. public Edge addEdge ( Vertex v0, Vertex v1); // Add to this graph a new edge connecting vertices // v0 and v1, but containing no attribute, and return // the new edge. (If the graph is directed, v0 is the // edge ’ s source and v1 is its destination.)

15 14-15 Graph ADT: contract (4)  Possible contract (continued): public Edge addEdge ( Vertex v0, Vertex v1, A attr); // Add to this graph a new edge connecting vertices // v0 and v1, and containing edge attribute attr, // and return the new edge. (If the graph is directed, // v0 is the edge ’ s source and v1 is its destination.) public void removeVertex (Vertex v); // Remove vertex v from this graph, together with all // its connecting edges. public void removeEdge (Edge e); // Remove edge e from this graph.

16 14-16 Graph ADT: contract (5)  Possible contract (continued): //////////// Iterators //////////// public Iterator vertices (); // Return an iterator that will visit all vertices of this // graph, in no particular order. public Iterator edges (); // Return an iterator that will visit all edges of this // graph, in no particular order.

17 14-17 Graph ADT: contract (6)  Possible contract (continued): public Iterator neighbours ( Vertex v); // Return an iterator that will visit all neighbours of // vertex v in this graph, in no particular order. public Iterator connectingEdges ( Vertex v); // Return an iterator that will visit all connecting edges // of vertex v in this graph, in no particular order.

18 14-18 Graph ADT: contract (7)  Possible contract (continued): //////// Inner interface for vertices //////// public interface Vertex { // Each Vertex object is a graph vertex, and // contains a single element. public E getElement (); // Return the element contained in this vertex. public void setElement (E elem); // Change the element contained in this vertex to // be elem. }

19 14-19 Graph ADT: contract (8)  Possible contract (continued): //////// Inner interface for edges //////// public interface Edge { // Each Edge object is a graph edge, and // optionally contains a single attribute. public A getAttribute (); // Return the attribute of this edge, or null if there // is none. public void setAttribute (A attr); // Change the attribute contained in this edge to // attr.

20 14-20 Graph ADT: contract (9)  Possible contract (continued): public Vertex[] getVertices (); // Return an array containing the two vertices // connected by this edge. (If the graph is directed, // the array will contain the edge ’ s source and // destination vertices in that order.) } }

21 14-21 Directed graph ADT: contract (1)  Possible contract for directed graphs: public interface Digraph extends Graph { // Each Digraph object is a directed graph // whose elements are of type E and whose edge // attributes are of type A. //////////// Accessor //////////// public int outDegree (Vertex v); // Return the number of out-edges of vertex v in this // graph.

22 14-22 Directed graph ADT: contract (2)  Possible contract (continued): //////////// Iterators //////////// public Iterator successors ( Vertex v); // Return an iterator that will visit all successors of // vertex v in this directed graph, in no particular // order. public Iterator outEdges ( Vertex v); // Return an iterator that will visit all out-edges of // vertex v in this directed graph, in no particular // order. }

23 14-23 Implementation of graphs using edge-sets (1)  Represent a graph by a vertex-set and an edge- set.  Make each edge contain links to the two vertices it connects.  Represent the vertex-set by a DLL. (This speeds up deletion of a given vertex.)  Represent the edge-set by a DLL. (This speeds up deletion of a given edge.)

24 14-24 Implementation of graphs using edge-sets (2)  Illustration: a b c d e f g edges S T U V W X vertices W abf e c d g V X TU W S elem. attr. link to source link to destination links to other edges links to other vertices Key: vertex edge

25 14-25 Implementation of graphs using edge-sets (3)  Summary of algorithms (letting n e be the number of edges): OperationAlgorithm Time complexity containsEdge linear search through edge-set DLLO(ne)O(ne) addVertex insertion at front of vertex-set DLLO(1) addEdge insertion at front of edge-set DLLO(1) removeVertex deletion in vertex-set DLL, plus multiple deletions in edge-set DLL O(ne)O(ne) removeEdge deletion in edge-set DLLO(1)

26 14-26 Implementation of graphs using edge-sets (4)  If the graph is directed: –The outEdges and successors iterators must traverse the entire edge-set to find all the given vertex’s out-edges and successors. So both iterators have time complexity O(n e ).  How would this be affected if the graph is undirected?

27 14-27 Implementation of graphs using adjacency-sets (1)  Represent a graph by a vertex-set together with an adjacency-set for each vertex. –Each adjacency-set contains only the edges connecting a particular vertex.  Make each vertex contain a link to its adjacency- set.  Make each edge contain links to the two vertices it connects.  Represent the vertex-set by a DLL (as before).  Represent each adjacency-set by a SLL (since it is typically small).

28 14-28 Implementation of graphs using adjacency-sets (2)  Illustration: S T U V W X vertices b c f d e a g elem. attr. link to source link to destination link to another edge links to other vertices Key: vertex edge link to adj. set W abf e c d g V X TU W S

29 14-29 Implementation of graphs using adjacency matrices (3)  Summary of algorithms (letting n e be the number of edges, and letting d be the maximum degree of a vertex): OperationAlgorithm Time complexity containsEdge linear search through adjacency-set SLLO(d)O(d) addVertex insertion at front of vertex-set DLLO(1) addEdge insertion at front of adjacency-set SLLO(1) removeVertex deletion in vertex-set DLL, plus traversal of all adjacency-set SLLs to find and delete connecting edges O(ne)O(ne) removeEdge deletion in adjacency-set SLLO(d)O(d)

30 14-30 Implementation of graphs using adjacency-sets (4)  If the graph is directed: –Store only the out-edges in each vertex’s adjacency- set. (So each edge is stored only once.) –The outEdges and successors iterators need traverse only the given vertex’s adjacency-set. So both iterators have time complexity O(d).  How would this be affected if the graph is undirected?

31 14-31 Implementation of bounded graphs using adjacency-matrices (1)  Represent a bounded graph (size  m) by an m × m matrix. Allocate a unique number in the range 0 … m–1 to each vertex.  The vertex-set is represented by an array a, indexed by vertex numbers, containing links to the adjacency-sets.  Each vertex’s adjacency-set is itself represented by an array, indexed by vertex numbers.  If vertex v is connected to vertex w, then a[v][w] contains a link to the edge object.

32 14-32 Implementation of bounded graphs using adjacency-matrices (2)  Illustration (with m = 10): S 0 T 1 U 2 V 3 W 4 X 5 6 7 8 9 0 1 2 3 4 5 6 78 9 b c f d e ag 0 1 2 3 4 2 01 2 3 4 1 5 5 Key: source dest. attr. elem. vertex edge link to adj. set W abf e c d g V X TU W S

33 14-33 Implementation of bounded graphs using adjacency-matrices (3)  Summary of algorithms: OperationAlgorithm Time complexity containsEdge matrix indexingO(1) addVertex finding a cleared matrix row and columnO(m)O(m) addEdge matrix indexingO(1) removeVertex clearing a matrix row and columnO(m)O(m) removeEdge matrix indexingO(1)

34 14-34 Implementation of bounded graphs using adjacency-matrices (4)  Advantages and disadvantages of the adjacency- matrix representation: + The containsEdge, addEdge and removeEdge operations are very fast. – The space required is about m 2. This is wasteful if there are relatively few edges. – The graph is restricted to at most one edge connecting any pair of vertices.

35 14-35 Implementation of bounded graphs using adjacency-matrices (5)  If the graph is directed: –If an edge connects vertex v to vertex w, store a link to the edge object in a[v][w] but not in a[w][v]. This reflects the edge’s direction. –The outEdges and successors iterators must traverse a whole row of the matrix. So both iterators have time complexity O(m).  How would this be affected if the graph is undirected?

36 14-36 Graph traversal (1)  A graph traversal problem entails finding all vertices that can be reached from a given starting vertex. E.g.: –Find all places that can be reached by road from Glasgow.  Algorithms to solve such problems must follow all possible paths from the starting vertex, visiting each vertex reached along these paths.

37 14-37 Graph traversal (2)  A graph traversal algorithm could reach the same vertex along several different paths.  So it must “mark” each vertex the first time it is reached, to avoid visiting the same vertex repeatedly. (Otherwise traversal of a cyclic graph would never terminate.)  Alternative ways to mark vertices: –Store a boolean flag in each vertex, initially false. To mark a vertex, set this flag to true. –Maintain a separate set of vertices, initially empty. To mark a vertex, add it to this set.

38 14-38 Depth-first traversal (1)  Depth-first traversal will traverse one possible path as far as possible, before traversing any alternative paths.  Illustration (starting from London): London Manchester Birmingham Bristol Swansea Liverpool Leeds Rugby Dublin Belfast Liverpool √ Possible order of visits: √ London √ Bristol √ Swansea √ Birmingham √ Manchester √ Leeds √ Rugby

39 14-39 Depth-first traversal (2)  Depth-first graph traversal algorithm: To traverse graph g in depth-first order, starting at vertex start: 1.Make vertex-stack contain only vertex start, and mark start as reached. 2.While vertex-stack is not empty, repeat: 2.1.Remove the top element of vertex-stack into v. 2.2.Visit vertex v. 2.3.For each unreached successor w of vertex v, repeat: 2.3.1.Add vertex w to vertex-stack, and mark w as reached. 3.Terminate.

40 14-40 Breadth-first traversal (1)  Breadth-first traversal will visit all successors of a vertex, before visiting any of their successors. It will traverse shorter paths before longer paths.  Illustration (starting from London): Liverpool √ Possible order of visits: √ London √ Bristol √ Swansea √ Birmingham √ Manchester √ Leeds √ Rugby London Manchester Birmingham Bristol Swansea Liverpool Leeds Rugby Dublin Belfast

41 14-41 Breadth-first traversal (2)  Breadth-first graph traversal algorithm: To traverse graph g in breadth-first order, starting at vertex start: 1.Make vertex-queue contain only vertex start, and mark start as reached. 2.While vertex-queue is not empty, repeat: 2.1.Remove the front element of vertex-queue into v. 2.2.Visit vertex v. 2.3.For each unreached successor w of vertex v, repeat: 2.3.1.Add vertex w to vertex-queue, and mark w as reached. 3.Terminate.

42 14-42 Graph search (1)  A graph search problem entails finding a path between two given vertices. E.g.: –Is there a road route from Glasgow to Dublin? –What is the length of the shortest route from Glasgow to London?  Graph search algorithms are similar to graph traversal algorithms, except that they terminate as soon as a solution is found.

43 14-43 Graph search (2)  Depth-first search will explore one possible path as far as possible, before exploring any alternative paths.  Breadth-first search will explore shorter paths before longer paths.

44 14-44 Shortest path (1)  Problem: Given a road network, find the shortest path between place start and place dest.  Idea: –Use breadth-first search. –For each place p, let pred[p] be the predecessor of p on the shortest path so far found from start to p, and let dist[p] be the distance along that path. –Initialize each dist[p] to  (infinity). –Thereafter reduce dist[p] (and update pred[p]) whenever a shorter path to p is found.

45 14-45 Shortest path (2)  Shortest-path algorithm: To find the shortest path through a road network from place start to place dest: 1.Make places contain all places in the road network. 2.Set dist[start] to 0 and set pred[start] to none. 3.For each place p other than start: 3.1.Set dist[p] to  and set pred[p] to none. 4.…

46 14-46 Shortest path (3)  Shortest-path algorithm (continued): 4.While places is not empty, repeat: 4.1.Remove from places the place p with least dist[p]. 4.2.If p = dest: 4.2.1.Terminate with the path «start, …, pred[pred[dest]], pred[dest], dest». 4.3.For each road connecting p and another place q, such that q is in places, repeat: 4.3.1.Let d be dist[p] + (distance from p to q). 4.3.2.If d < dist[q], set dist[q] to d and set pred[q] to p. 5.Terminate with no path. At this point, we have already found the shortest path to dest.

47 14-47 Shortest path (4)  Illustration – shortest path from Lo(ndon) to Le(eds): 170 130 140 70 150 220 190 60 Lo Ma Bi Br Le Ru BiBrLeLoMaRuplaces none,  none,  none,  none,0none,  none,  {Bi,Br,Le,Lo,Ma,Ru} Lo,220Lo,190none,  none,0none,  Lo,150{Bi,Br,Le,Ma,Ru} Ru,210Lo,190Ru,320none,0none,  Lo,150{Bi,Le,Ma} Ru,210Lo,190Ru,320none,0Bi,340Lo,150{Le,Ma} Shortest path is «Lo, Ru, Le». Ru,210Lo,190Ru,320none,0none,  Lo,150{Bi,Br,Le,Ma} pred[Bi] dist[Bi]


Download ppt "14 Graph ADTs  Graph concepts.  Graph applications.  A graph ADT: requirements, contract.  Implementations of graphs: edge-set, adjacency-set, adjacency-matrix."

Similar presentations


Ads by Google