Algorithms Data Structures M 14 Graph ADTs Graph

  • Slides: 47
Download presentation
Algorithms & Data Structures (M) 14 Graph ADTs § Graph concepts. § Graph applications.

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

Graph concepts (1) § A graph consists of vertices connected by edges in an

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

Graph concepts (2) § In an undirected graph, edges have no direction: vertices S

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

Graph concepts (3) § A path in a graph is a list of vertices,

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 W V T U X «S, T, W, V» is a path. S W V T U X «S, T, U, V» is a path. «S, T, W, V» is not a path. 14 -4

Graph applications § A road network is an undirected graph in which each vertex

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. 14 -5

Example: road network § Simplified road network for GB and Ireland: Glasgow 150 70

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

Graph concepts (4) § A cycle in a graph is a path whose first

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. W V S T U X A B C D This graph is acyclic. This graph is cyclic, since «T, U, V, W, T» is a cycle. 14 -7

Graph terminology (1) § Two vertices of a graph are neighbours if they are

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. 14 -8

Graph terminology (2) § In a directed graph, if e is an edge connecting

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. 14 -9

Graph ADT: requirements (1) § Requirements (for both undirected and directed graphs): 1) It

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. 14 -10

Graph ADT: requirements (2) § Requirements (continued): 7) It must be possible to inspect

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 outedges, of a given vertex in a directed graph. § Note: Vertices and edges must be visible outside the graph ADT. 14 -11

Graph ADT: contract (1) § Possible contract for all graphs: public interface Graph<E, A>

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

Graph ADT: contract (2) § Possible contract (continued): ////// Accessors ////// public int size

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 contains. Edge ( Vertex v 0, Vertex v 1); // Return true if and only if these is an edge // connecting vertices v 0 and v 1 in this graph. (If the // graph is directed, v 0 is the edge’s source and v 1 is // its destination. ) 14 -13

Graph ADT: contract (3) § Possible contract (continued): ////// Transformers ////// public void clear

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

Graph ADT: contract (4) § Possible contract (continued): public Edge add. Edge ( Vertex

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

Graph ADT: contract (5) § Possible contract (continued): ////// Iterators ////// public Iterator<Vertex> vertices

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

Graph ADT: contract (6) § Possible contract (continued): public Iterator<Vertex> neighbours ( Vertex v);

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

Graph ADT: contract (7) § Possible contract (continued): //// Inner interface for vertices ////

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 get. Element (); // Return the element contained in this vertex. public void set. Element (E elem); // Change the element contained in this vertex to // be elem. } 14 -18

Graph ADT: contract (8) § Possible contract (continued): //// Inner interface for edges ////

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 get. Attribute (); // Return the attribute of this edge, or null if there // is none. public void set. Attribute (A attr); // Change the attribute contained in this edge to // attr. 14 -19

Graph ADT: contract (9) § Possible contract (continued): public Vertex[] get. Vertices (); //

Graph ADT: contract (9) § Possible contract (continued): public Vertex[] get. Vertices (); // 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. ) } } 14 -20

Directed graph ADT: contract (1) § Possible contract for directed graphs: public interface Digraph<E,

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

Directed graph ADT: contract (2) § Possible contract (continued): ////// Iterators ////// public Iterator<Vertex>

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

Implementation of graphs using edge-sets (1) § Represent a graph by a vertex-set and

Implementation of graphs using edge-sets (1) § Represent a graph by a vertex-set and an edgeset. § 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. ) 14 -23

Implementation of graphs using edge-sets (2) § Illustration: W e S a T b

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

Implementation of graphs using edge-sets (3) § Summary of algorithms (letting ne be the

Implementation of graphs using edge-sets (3) § Summary of algorithms (letting ne be the number of edges): Operation Algorithm Time complexity contains. Edge linear search through edge-set DLL O(ne) add. Vertex insertion at front of vertex-set DLL O(1) add. Edge insertion at front of edge-set DLL O(1) remove. Vertex deletion in vertex-set DLL, plus multiple deletions in edge-set DLL remove. Edge deletion in edge-set DLL O(ne) O(1) 14 -25

Implementation of graphs using edge-sets (4) § If the graph is directed: – The

Implementation of graphs using edge-sets (4) § If the graph is directed: – The out. Edges 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(ne). § How would this be affected if the graph is undirected? 14 -26

Implementation of graphs using adjacency-sets (1) § Represent a graph by a vertex-set together

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 adjacencyset. § 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). 14 -27

Implementation of graphs using adjacency-sets (2) § Illustration: W e S a d V

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

Implementation of graphs using adjacency matrices (3) § Summary of algorithms (letting ne be

Implementation of graphs using adjacency matrices (3) § Summary of algorithms (letting ne be the number of edges, and letting d be the maximum degree of a vertex): Time complexity Operation Algorithm contains. Edge linear search through adjacency-set SLL O(d) add. Vertex insertion at front of vertex-set DLL O(1) add. Edge insertion at front of adjacency-set SLL O(1) remove. Vertex deletion in vertex-set DLL, plus traversal of all adjacency-set SLLs to find and delete connecting edges O(ne) remove. Edge deletion in adjacency-set SLL O(d) 14 -29

Implementation of graphs using adjacency-sets (4) § If the graph is directed: – Store

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 out. Edges 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? 14 -30

Implementation of bounded graphs using adjacency-matrices (1) § Represent a bounded graph (size m)

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. 14 -31

Implementation of bounded graphs using adjacency-matrices (2) § Illustration (with m = 10): W

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

Implementation of bounded graphs using adjacency-matrices (3) § Summary of algorithms: Time complexity Operation

Implementation of bounded graphs using adjacency-matrices (3) § Summary of algorithms: Time complexity Operation Algorithm contains. Edge matrix indexing O(1) add. Vertex finding a cleared matrix row and column O(m) add. Edge matrix indexing O(1) remove. Vertex clearing a matrix row and column O(m) remove. Edge matrix indexing O(1) 14 -33

Implementation of bounded graphs using adjacency-matrices (4) § Advantages and disadvantages of the adjacencymatrix

Implementation of bounded graphs using adjacency-matrices (4) § Advantages and disadvantages of the adjacencymatrix representation: + The contains. Edge, add. Edge and remove. Edge 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. 14 -34

Implementation of bounded graphs using adjacency-matrices (5) § If the graph is directed: –

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 out. Edges 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? 14 -35

Graph traversal (1) § A graph traversal problem entails finding all vertices that can

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. 14 -36

Graph traversal (2) § A graph traversal algorithm could reach the same vertex along

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. 14 -37

Depth-first traversal (1) § Depth-first traversal will traverse one possible path as far as

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): Belfast √ Liverpool Dublin √ Manchester √ Birmingham √ Swansea √ Bristol Possible order of visits: √ Leeds √ Rugby √ London Bristol Swansea Birmingham Manchester Liverpool Leeds Rugby 14 -38

Depth-first traversal (2) § Depth-first graph traversal algorithm: To traverse graph g in depth-first

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. 14 -39

Breadth-first traversal (1) § Breadth-first traversal will visit all successors of a vertex, before

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): Belfast √ Liverpool Dublin √ Manchester √ Birmingham √ Swansea √ Bristol Possible order of visits: √ Leeds √ Rugby √ London Bristol Birmingham Rugby Swansea Manchester Leeds Liverpool 14 -40

Breadth-first traversal (2) § Breadth-first graph traversal algorithm: To traverse graph g in breadth-first

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. 14 -41

Graph search (1) § A graph search problem entails finding a path between two

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. 14 -42

Graph search (2) § Depth-first search will explore one possible path as far as

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. 14 -43

Shortest path (1) § Problem: Given a road network, find the shortest path between

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. 14 -44

Shortest path (2) § Shortest-path algorithm: To find the shortest path through a road

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. … 14 -45

Shortest path (3) § Shortest-path algorithm (continued): 4. While places is not empty, repeat:

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[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. 14 -46

Shortest path (4) Ma § Illustration – shortest path from Lo(ndon) to Le(eds): 140

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