47
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)

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

Embed Size (px)

Citation preview

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

14Graph 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)

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

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

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

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

XT U

W

S

W V

XT U

W

S

undirected edgesvertices

directed edges

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

14-4

W V

XT U

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

XT U

W

S

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

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.

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

14-6

Example: road network

Simplified road network for GB and Ireland:

140 160

170

150

190

130

140

60 70 100

100150220

160 190120110120

70

60

London

EdinburghGlasgow

Carlisle

Manchester

Birmingham

Bristol

Exeter

Swansea

Liverpool Leeds

Rugby

Hull

Cambridge

DoverSouthampton

Newcastle

Dublin

Belfast

«Edinburgh, Newcastle, Leeds»is not a path.

«Dover, London, Bristol, Exeter»is a path.

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

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

XT U

W

S

This graph is acyclic.DCA B

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

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

XT U

W

S

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

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

v w

W V

XT U

W

S

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

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.

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

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.

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

14-12

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.

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

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

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

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

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

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.

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

14-16

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.

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

14-17

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> connectingEdges (Vertex v);

// Return an iterator that will visit all connecting edges

// of vertex v in this graph, in no particular order.

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

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.

}

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

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.

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

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

}

}

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

14-21

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 outDegree (Vertex v);// Return the number of out-edges of vertex v in

this // graph.

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

14-22

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> outEdges (Vertex v);

// Return an iterator that will visit all out-edges of // vertex v in this directed graph, in no particular // order.

}

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

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

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

14-24

Implementation of graphs using edge-sets (2)

Illustration:

a

b

c

d

e

f

g

edges

S

T

U

V

W

X

verticesW

a b fe c

d

g

V

XT U

W

S

elem.

attr.link to sourcelink to destination

links to other edges

links to other vertices

Key:vertex

edge

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

14-25

Implementation of graphs using edge-sets (3)

Summary of algorithms (letting ne be the number of edges):

Operation AlgorithmTime

complexity

containsEdge linear search through edge-set DLL O(ne)

addVertex insertion at front of vertex-set DLL O(1)

addEdge insertion at front of edge-set DLL O(1)

removeVertex deletion in vertex-set DLL, plus multiple deletions in edge-set DLL

O(ne)

removeEdge deletion in edge-set DLL O(1)

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

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(ne).

How would this be affected if the graph is undirected?

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

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

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

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 sourcelink to destination link to

another edge

links to other vertices

Key: vertex

edge

link to adj. set

W

a b fe c

d

g

V

XT U

W

S

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

14-29

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):

Operation AlgorithmTime

complexity

containsEdge linear search through adjacency-set SLL O(d)

addVertex insertion at front of vertex-set DLL O(1)

addEdge insertion at front of adjacency-set SLL O(1)

removeVertex deletion in vertex-set DLL, plus traversal of all adjacency-set SLLs to find and delete connecting edges

O(ne)

removeEdge deletion in adjacency-set SLL O(d)

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

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?

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

14-31

Implementation of bounded graphsusing 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.

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

14-32

Implementation of bounded graphsusing adjacency-matrices (2)

Illustration (with m = 10):

S0T1U2V3W4X5

6789

0 1 2 3 4 5 6 7 8 9

b

c

f

d

e

a g0

1

2

3

4

2

01

2

3

4

1

5

5Key:

source dest. attr.

elem.vertex

edge

link to adj. set

W

a b fe c

d

g

V

XT U

W

S

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

14-33

Implementation of bounded graphs using adjacency-matrices (3)

Summary of algorithms:

Operation AlgorithmTime

complexity

containsEdge matrix indexing O(1)

addVertex finding a cleared matrix row and column O(m)

addEdge matrix indexing O(1)

removeVertex clearing a matrix row and column O(m)

removeEdge matrix indexing O(1)

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

14-34

Implementation of bounded graphsusing 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 m2. This is wasteful if there are relatively few edges.

– The graph is restricted to at most one edge connecting any pair of vertices.

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

14-35

Implementation of bounded graphsusing 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?

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

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.

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

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.

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

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

BristolSwansea

Liverpool Leeds

RugbyDublin

Belfast

Liverpool

√Possible order of visits:

London

Bristol

Swansea√

Birmingham

Manchester

Leeds

Rugby

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

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.

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

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

BristolSwansea

Liverpool Leeds

RugbyDublin

Belfast

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

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.

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

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.

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

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.

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

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.

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

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

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

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.

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

14-47

Shortest path (4)

Illustration –shortest path from Lo(ndon) to Le(eds):

170130

140

70

150220

190

60

Lo

Ma

Bi

Br

Le

Ru

Bi Br Le Lo Ma Ru places

none, none, none, none,0 none, none, {Bi,Br,Le,Lo,Ma,Ru}

Lo,220 Lo,190 none, none,0 none, Lo,150 {Bi,Br,Le,Ma,Ru}

Ru,210 Lo,190 Ru,320 none,0 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».

Ru,210 Lo,190 Ru,320 none,0 none, Lo,150 {Bi,Br,Le,Ma}

pred[Bi]dist[Bi]