Top Banner
Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes dified from authors’ slides
122

Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Dec 16, 2015

Download

Documents

Eleanor Barton
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Chapter 20:

Graphs

CS 302 - Data StructuresMehmet H Gunes

Modified from authors’ slides

Page 2: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

What is a graph?

• A data structure that consists of a set of nodes (vertices) and a set of edges between the vertices.

• The set of edges describes relationships among the vertices.

1 2

3 4

Page 3: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Terminology

• Definition:– A set of points that are joined by lines

• Graphs also represent the relationships among data items

• G = { V , E }– a graph is a set of vertices and edges

• A subgraph consists of a subset of a graph’s vertices and a subset of its edges

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 4: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Formally

• a graph G is defined as follows:

G = (V,E)

• where– V(G) is a finite, nonempty set of vertices– E(G) is a set of edges

• written as pairs of vertices

Page 5: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

An undirected graph

The order of vertices in E is not important forundirected graphs!!

A graph in which the edges have no direction

Page 6: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

A directed graph

A graph in which each edge is directed from one vertex to another (or the same) vertex

The order of vertices in E is important fordirected graphs!!

Page 7: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

A directed graph

Trees are special cases of graphs!

Page 8: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Terminology

• Undirected graphs: edges do not indicate a direction

• Directed graph, or digraph: each edge has a direction

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 9: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Terminology

• (a) A campus map as a graph; (b) a subgraph

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 10: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Terminology

• Graphs that are (a) connected; (b) disconnected; and (c) complete

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 11: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Terminology

• (a) A multigraph is not a simple graph; (b) a self edge is not allowed in a simple graph

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 12: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

• Path: A sequence of vertices that connects two nodes in a graph

• The length of a path is the number of edges in the path.

e.g., a path from 1 to 4

1 2

3 4

<1, 2, 3, 4>

Graph terminology

Page 13: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Terminology

• Simple path: passes through vertex only once• Cycle: a path that begins and ends at same

vertex• Simple cycle: cycle that does not pass through

other vertices more than once• Connected graph: each pair of distinct vertices

has a path between them

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 14: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Graph terminology

Complete graph: A graph in which every vertex is directly connected to every other vertex

Page 15: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Terminology

• Complete graph: each pair of distinct vertices has an edge between them

• Graph cannot have duplicate edges between vertices– Multigraph: does allow multiple edges

• When labels represent numeric values, graph is called a weighted graph

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 16: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

• What is the number of edges E in a complete undirected graph with V vertices?

E=V* (V-1) / 2

Graph terminology (cont.)

or O(V2)

Page 17: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

• What is the number of edges E in a complete directed graph with V vertices?

E=V * (V-1)

Graph terminology (cont.)

or O(V2)

Page 18: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

A weighted graph

Weighted graph: A graph in which each edge carries a value

18

Page 19: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Terminology

• (a) a weighted graph; (b) a directed graphData Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 20: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Graphs as ADTs

ADT graph operations– Test whether graph is empty.– Get number of vertices in a graph.– Get number of edges in a graph.– See whether edge exists between two given

vertices.– Insert vertex in graph whose vertices have distinct

values that differ from new vertex’s value.– Insert edge between two given vertices in graph.

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 21: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Graphs as ADTs

ADT graph operations, ctd.– Remove specified vertex from graph and any

edges between the vertex and other vertices.– Remove edge between two vertices in graph.– Retrieve from graph vertex that contains given

value.

•View interface for undirected, connected graphs, Listing 20-1

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 22: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Array-Based Implementation

• Use a 1D array to represent the vertices• Use a 2D array (i.e., adjacency matrix) to

represent the edges

• Adjacency Matrix: – for a graph with N nodes,

• an N by N table that shows the existence (and weights) of all edges in the graph

Page 23: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

to node x ?

from node x ?

Adjacency Matrix for Flight Connections

Page 24: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Array-Based Implementation (cont.)

• Memory required– O(V+V2)=O(V2)

• Preferred when– The graph is dense: E = O(V2)

• Advantage– Can quickly determine if there is an edge between two vertices

• Disadvantage– Consumes significant memory for sparse large graphs

Page 25: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Linked Implementation

• Use a 1D array to represent the vertices • Use a list for each vertex v which contains the

vertices which are adjacent from v (i.e., adjacency list)

• Adjacency List: – A linked list that identifies all the vertices to which a

particular vertex is connected; • each vertex has its own adjacency list

Page 26: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Adjacency List Representation of Graphs

to node x ?

from node x ?

Page 27: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Link-List-based Implementation (cont.)

• Memory required– O(V + E)

• Preferred when– for sparse graphs: E = O(V)

• Disadvantage– No quick way to determine the

vertices adjacent to a given vertex

• Advantage– Can quickly determine the vertices adjacent from a given vertex

O(V) for sparse graphs since E=O(V)

O(V2) for dense graphs since E=O(V2)

Page 28: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Implementing Graphs

• (a) A directed graph and (b) its adjacency matrix

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 29: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Implementing Graphs

• (a) A weighted undirected graph and (b) its adjacency matrix

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 30: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Implementing Graphs

• (a) A directed graph and (b) its adjacency list

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 31: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Implementing Graphs

• (a) A weighted undirected graph and (b) its adjacency list

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 32: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Graph Traversals

• Visits all of the vertices that it can reach– Happens if graph is connected

• Connected component is subset of vertices visited during traversal that begins at given vertex

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 33: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Graph searching

• Problem: find if there is a path between two vertices of the graph – e.g., Austin and Washington

• Methods: Depth-First-Search (DFS) or Breadth-First-Search (BFS)

Page 34: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Depth-First-Search (DFS)

• Main idea:– Travel as far as you can down a path – Back up as little as possible when you reach a

"dead end“• i.e., next vertex has been "marked" or there is no next

vertex

startVertex endVertex

Page 35: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Depth First Search: Follow Down

DFS uses Stack !

2

1

3

Page 36: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

startVertex endVertex

(initialization) 36

Page 37: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

37

Page 38: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

endVertex

38

Page 39: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Depth-First Search• Goes as far as possible from a vertex before backing

up• Recursive algorithm

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 40: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Depth-First Search• Iterative algorithm, using a stack

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 41: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Depth-First Search• Iterative algorithm, using a stack, ctd.

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 42: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Depth-First Search

• Visitation order for (a) a depth-first search; (b) a breadth-first search

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 43: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Depth-First Search

• A connected graph with cycles

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 44: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Depth-First Search

• The results of a depth-first traversal, beginning at vertex a, of the graph

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 45: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Breadth-First-Searching (BFS)

• Main idea:– Look at all possible paths at the same depth

before you go at a deeper level– Back up as far as possible when you reach a

"dead end“• i.e., next vertex has been "marked" or there is no next

vertex

startVertex endVertex

Page 46: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Breadth First: Follow Across

BFS uses Queue !

1

2

3

4

5

6

78

Page 47: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Breadth First Uses Queue

Page 48: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

startVertex endVertex

(initialization)

48

Page 49: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

49

Page 50: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

50

Page 51: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Breadth-First Search

• Visits all vertices adjacent to vertex before going forward

• Breadth-first search uses a queue

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 52: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Breadth-First Search

• The results of a breadth-fi rst traversal, beginning at vertex a, of the graph

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 53: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Applications of Graphs

• A directed graph without cycles• Topological Sorting

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 54: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Applications of Graphs

• The graph arranged according to the topological orders (a) a, g, d, b, e, c, f and (b) a, b, g, d, e, f, c

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 55: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Applications of Graphs

• Topological sorting algorithm

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 56: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Applications of Graphs

• A trace of topSort1 for the graphData Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 57: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Applications of Graphs

• A trace of topSort1 for the graphData Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 58: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Applications of Graphs

• A trace of topSort1 for the graphData Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 59: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Applications of Graphs

• A trace of topSort1 for the graphData Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 60: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Applications of Graphs

• A trace of topSort2 for the graphData Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 61: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Spanning Trees

• Tree: an undirected connected graph without cycles

• Observations about undirected graphs1. Connected undirected graph with n vertices

must have at least n – 1 edges.2. Connected undirected graph with n vertices and

exactly n – 1 edges cannot contain a cycle3. A connected undirected graph with n vertices

and more than n – 1 edges must contain at least one cycle

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 62: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Spanning Trees

• The DFS spanning tree rooted at vertex a for the graph

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 63: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Spanning Trees

• DFS spanning tree algorithm

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 64: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Spanning Trees

• BFS spanning tree algorithm

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 65: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Spanning Trees

• The BFS spanning tree rooted at vertex a for the graph

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 66: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Minimum Spanning Trees

• A weighted, connected, undirected graph

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 67: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Minimum Spanning Trees

• Minimum spanning tree algorithm

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 68: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Minimum Spanning Trees

• A trace of primsAlgorithm for the graph, beginning at vertex a

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 69: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Minimum Spanning Trees

• A trace of primsAlgorithm for the graph, beginning at vertex a

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 70: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Minimum Spanning Trees

• A trace of primsAlgorithm for the graph, beginning at vertex a

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 71: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Graph Algorithms

• Depth-first search – Visit all the nodes in a branch to its deepest point

before moving up • Breadth-first search

– Visit all the nodes on one level before going to the next level

• Single-source shortest-path

– Determines the shortest path from a designated starting node to every other node in the graph

71

Page 72: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Single Source Shortest Path

72

Page 73: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Single Source Shortest Path

• What does “shortest” mean?

• What data structure should you use?

73

Page 74: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Shortest-path problem

• There might be multiple paths from a source vertex to a destination vertex

• Shortest path: the path whose total weight (i.e., sum of edge weights) is minimum

AustinHoustonAtlantaWashington: 1560 miles AustinDallasDenverAtlantaWashington:

2980 miles74

Page 75: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Variants of Shortest Path

• Single-pair shortest path– Find a shortest path from u to v

• for given vertices u and v

• Single-source shortest paths– G = (V, E) find a shortest path from a given source

vertex s to each vertex v V

75

Page 76: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Variants of Shortest Paths (cont’d)

• Single-destination shortest paths– Find a shortest path to a given destination vertex t

from each vertex v– Reversing the direction of each edge single-source

• All-pairs shortest paths– Find a shortest path from u to v for every pair of

vertices u and v

76

Page 77: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Shortest Paths

• Shortest path between two vertices in a weighted graph has smallest edge-weight sum

(a) A weighted directed graph and (b) its adjacency matrix

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 78: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

• Weight of path p = v0, v1, . . . , vk

• Shortest-path weight from s to v:

min w(p) : s v if there exists a path from s to

v

∞ otherwise

Notation

k

iii vvwpw

11 ),()(

78

0

3 9

5 11

3

6

5

7

6

s

t x

y z

22 1

4

3

p

δ(v) =

Page 79: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Negative Weights and Negative Cycles

• Negative-weight edges may form negative-weight cycles.

• If negative cycles are reachable from the source, the shortest path is not well defined.– i.e., keep going around the cycle, and get

w(s, v) = - for all v on the cycle

0

3

-4

2

8

-6

s

a b

e f

-33

56

4

7

c d g

79

Page 80: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Could shortest path solutions contain cycles?

• Negative-weight cycles– Shortest path is not well defined

• Positive-weight cycles:– By removing the cycle, we can get a shorter path

• Zero-weight cycles

– No reason to use them; can remove them to obtain a path with same weight

80

Page 81: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Shortest-path algorithms• Solving the shortest path problem in a brute-force

manner requires enumerating all possible paths.– There are O(V!) paths between a pair of vertices in an

acyclic graph containing V nodes.

• We will discuss two algorithms– Dijkstra’s algorithm– Bellman-Ford’s algorithm

81

Page 82: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Shortest-path algorithms

• Dijkstra’s and Bellman-Ford’s algorithms are “greedy” algorithms!– Find a “globally” optimal solution by making “locally”

optimum decisions.

• Dijkstra’s algorithm– Does not handle negative weights.

• Bellman-Ford’s algorithm– Handles negative weights but not negative cycles reachable

from the source.

82

Page 83: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Shortest-path algorithms (cont’d)

• Both Dijkstra’s and Bellman-Ford’s algorithms are iterative:

– Start with a shortest path estimate for every vertex: d[v]

– Estimates are updated iteratively until convergence:

d[v]δ(v)

83

Page 84: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Shortest-path algorithms (cont’d)

• Two common steps:

(1) Initialization

(2) Relaxation (i.e., update step)

84

Page 85: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

0

6

5

7

7

9

s

t x

8-3

2-4

-2

Initialization Step• Set d[s]=0

– i.e., source vertex

• Set d[v]=∞ for– i.e., large value

v s

85

Page 86: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Relaxing an edge (u, v) implies testing whether we can improve the shortest path to v found so far by going through u:

If d[v] > d[u] + w(u, v)

we can improve the shortest path to v d[v]=d[u]+w(u,v)

Relaxation Step

5 92

u v

5 72

u v

RELAX(u, v, w)

5 62

u v

5 62

u v

RELAX(u, v, w)

s s

no change

86

Page 87: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Bellman-Ford Algorithm• Can handle negative weights.

• Detects negative cycles reachable from the source.

• Returns FALSE if negative-weight cycles are reachable from the source s no solution

87

Page 88: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Bellman-Ford Algorithm (cont’d)• Each edge is relaxed |V–1| times by making |V-1|

passes over the whole edge set– to make sure that each edge is relaxed exactly |V – 1| times

• it puts the edges in an unordered list and goes over the list |V – 1| times

0

6

5

7

7

9

s

t x

y z

8-3

2-4

-2

(t, x), (t, y), (t, z), (x, t), (y, x), (y, z), (z, x), (z, s), (s, t), (s, y)

88

Page 89: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Example

0

6

5

7

7

9

s

t x

y z

8-3

2-4

-2

0

6

5

7

7

9

s

t x

y z

8-3

2-4

-2

E: (t, x), (t, y), (t, z), (x, t), (y, x), (y, z), (z, x), (z, s), (s, t), (s, y)

6

7

Pass 1

89

Page 90: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Example

0

6

7

6

5

7

7

9

s

t x

y z

8-3

2-4

-2

(t, x), (t, y), (t, z), (x, t), (y, x), (y, z), (z, x), (z, s), (s, t), (s, y)

0

6

7

6

5

7

7

9

s

t x

y z

8-3

2-4

-211

2

4

0

6

7

6

5

7

7

9

s

t x

y z

8-3

2-4

-211

2

42

0

6

7

6

5

7

7

9

s

t x

y z

8-3

2-4

-211

2

42

-2

Pass 1(from previousslide)

Pass 2

Pass 3 Pass 4

90

Page 91: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Detecting Negative Cycles: needs an extra iteration

for each edge (u, v) E doif d[v] > d[u] + w(u, v) then return FALSE

return TRUE

0

c

s b2

3-8

0

c

s b2

3-8

2

5

-3 -3 2

5

c

s b2

3-8

-1

2

-6

Consider edge (s, b):

d[b] = -1d[s] + w(s, b) = -4

d[b] > d[s] + w(s, b) d[b]=-4 (d[b] keeps changing!)

1st pass 2nd pass

(s,b) (b,c) (c,s)

91

Page 92: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

BELLMAN-FORD Algorithm1. INITIALIZE-SINGLE-SOURCE(V, s)2. for i ← 1 to |V| - 13. for each edge (u, v) E4. RELAX(u, v, w)5. for each edge (u, v) E6. if d[v] > d[u] + w(u, v)7. return FALSE8. return TRUE Time: O(V+VE+E)=O(VE)

O(V)

O(V)

O(E)

O(E)

O(VE)

92

Page 93: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Dijkstra’s Algorithm

• Cannot handle negative-weights!– w(u, v) > 0, (u, v) E

• Each edge is relaxed only once!

93

Page 94: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Dijkstra’s Algorithm (cont’d)

• At each iteration, it maintains two sets of vertices:

d[v]=δ (v) d[v]≥δ (v)

V

S V-S

estimates have converged to the shortest path solution

estimates have notconverged yet

Initially, S is empty94

Page 95: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Dijkstra’s Algorithm (cont.)

• Vertices in V–S reside in a min-priority queue Q

– Priority of u determined by d[u]

– The “highest” priority vertex will be the one having the

smallest d[u] value.

95

Page 96: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Dijkstra (G, w, s)

0

10

1

5

2

s

t x

y z

2 3

9

7

4 6 0

10

1

5

2

s

t x

y z

2 3

9

7

4 6

10

5

S=<> Q=<s,t,x,z,y> S=<s> Q=<y,t,x,z>

Initialization

96

Page 97: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Example (cont.)

0

10

5

10

1

5

2

s

t x

y z

2 3

9

7

4 6

8 14

7

0

8 14

5 7

10

1

5

2

s

t x

y z

2 3

9

7

4 6

13

S=<s,y> Q=<z,t,x> S=<s,y,z> Q=<t,x>

97

Page 98: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Example (cont.)

0

8 13

5 7

10

1

5

2

s

t x

y z

2 3

9

7

4 6

9

0

8 9

5 7

10

1

5

2

s

t x

y z

2 3

9

7

4 6

S=<s,y,z,t> Q=<x> S=<s,y,z,t,x> Q=<>

Note: use back-pointers to recover the shortest path solutions!

98

Page 99: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Dijkstra (G, w, s) INITIALIZE-SINGLE-SOURCE(V, s)

S ←

Q ← V[G]

while Q

u ← EXTRACT-MIN(Q)

S ← S {u}

for each vertex v Adj[u]

RELAX(u, v, w)

Update Q (DECREASE_KEY)

Overall: O(V+2VlogV+(Ev1+Ev2+...)logV)

=O(VlogV+ElogV)=O(ElogV)

build priority heap

O(V logV)

O(V) times

O(logV)

O(Evi)

O(logV)

O(V)

O(Evi logV)

99

Page 100: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Improving Dijkstra’s efficiency• Suppose the shortest path from s to w is the

following:

• If u is the i-th vertex in this path, it can be shown that d[u] δ (u) at the i-th iteration:– move u from V-S to S– d[u] never changes again

w

s xu

… …

100

Page 101: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Add a flag for efficiency!

INITIALIZE-SINGLE-SOURCE(V, s)

S ←

Q ← V[G]

while Q

u ← EXTRACT-MIN(Q)

S ← S {u};

for each vertex v Adj[u]

RELAX(u, v, w)

Update Q (DECREASE_KEY)

If v not marked

mark u

101

Page 102: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Dijkstra vs Bellman-Ford

• Bellman-Ford O(VE)

• Dijkstra

O(ElogV)

V2

V3

if G is sparse: E=O(V)

if G is dense: E=O(V2)

VlogV

V2logV

if G is sparse: E=O(V)

if G is dense: E=O(V2)102

Page 103: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Shortest Paths

• Dijkstra’s shortest-path algorithm

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 104: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Shortest Paths

• Dijkstra’s shortest-path algorithm, ctd.

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 105: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Shortest Paths

• A trace of the shortest-path algorithm applied to the graph

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 106: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Shortest Paths

• Checking weight [u] by examining the graph: (a) weight [2] in step 2; (b) weight [1] in step 3;

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 107: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Shortest Paths

• Checking weight [u] by examining the graph: (c) weight [3] in step 3; (d) weight [3] in step 4

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 108: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

• BFS can be used to solve the shortest path problem when the graph is weightlessweightless or when all the weights are equal.– Path with lowest number of edges

• i.e., connections

• Need to “mark” vertices before Enqueue!– i.e., do not allow duplicates

Revisiting BFS

108

Page 109: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Circuits

• Another name for a type of cycle common in statement of certain problems

• Circuits either visit every vertex once or visit every edge once

• An Euler circuit begins at a vertex v, passes through every edge exactly once, and terminates at v

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 110: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Circuits

• (a) Euler’s bridge problem and (b) its multigraph representation

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 111: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Circuits

• Pencil and paper drawings

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 112: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Circuits

• Connected undirected graphs based on the drawings

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 113: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Circuits

• The steps to determine an Euler circuit for the graph

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 114: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Circuits

• The steps to determine an Euler circuit for the graph

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 115: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Some Difficult Problems

• Hamilton circuit– Path that begins at a vertex v, passes through

every vertex in the graph exactly once, and terminates at v

• The traveling salesperson problem– Variation of Hamilton circuit– Involves a weighted graph that represents a road

map– Circuit traveled must be the least expensive

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 116: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Some Difficult Problems

• The three utilities problem

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 117: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Some Difficult Problems

• Planar graph– Can draw it in a plane in at least one way so that

no two edges cross

• The four-color problem– Given a planar graph, can you color the vertices so

that no adjacent vertices have the same color, if you use at most four colors?

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 118: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Some Difficult Problems

1. Describe the graphs in Figure 20-32 . For example, are they directed? Connected? Complete? Weighted?

2. Use the depth-first strategy and the breadth-first strategy to traverse the graph in Figure 20-32 a, beginning with vertex 0. List the vertices in the order in which each traversal visits them.

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 119: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Some Difficult Problems

3. Write the adjacency matrix for the graph in Figure 20-32 a.

4. Add an edge to the directed graph in Figure 20-14 that runs from vertex d to vertex b. Write all possible topological orders for the vertices in this new graph.

5. Is it possible for a connected undirected graph with fi ve vertices and four edges to contain a simple cycle? Explain.

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 120: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Some Difficult Problems

6. Draw the DFS spanning tree whose root is vertex 0 for the graph in Figure 20-33 .

7. Draw the minimum spanning tree whose root is vertex 0 for the graph in Figure 20-33 .

8. What are the shortest paths from vertex 0 to each vertex of the graph in Figure 20-24 a? (Note the weights of these paths in Figure 20-25 .)

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 121: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Some Difficult Problems

• FIGURE 20-32 Graphs for Checkpoint Questions 1, 2, and 3

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013

Page 122: Chapter 20: Graphs CS 302 - Data Structures Mehmet H Gunes Modified from authors’ slides.

Some Difficult Problems

• FIGURE 20-33 A graph for Checkpoint Questions 6 and 7 and for Exercises 1 and 4

Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013