Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E}...

46
Chapter 14 Graphs

Transcript of Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E}...

Page 1: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

Chapter 14

Graphs

Page 2: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -2

Terminology

• G = {V, E}• A graph G consists of two sets

– A set V of vertices, or nodes– A set E of edges

• A subgraph– Consists of a subset of a graph’s vertices and a subset

of its edges

• Adjacent vertices– Two vertices that are joined by an edge

Page 3: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -3

Terminology

Figure 14.2Figure 14.2

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

Page 4: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -4

Terminology

• A path between two vertices– A sequence of edges that begins at one vertex and ends at

another vertex– May pass through the same vertex more than once

• A simple path– A path that passes through a vertex only once

• A cycle– A path that begins and ends at the same vertex

• A simple cycle– A cycle that does not pass through a vertex more than once

Page 5: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -5

Terminology

• A connected graph– A graph that has a path between each pair of distinct

vertices

• A disconnected graph– A graph that has at least one pair of vertices without a

path between them

• A complete graph– A graph that has an edge between each pair of distinct

vertices

Page 6: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -6

Terminology

Figure 14.3Figure 14.3

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

Page 7: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -7

Terminology

• Multigraph– Allows multiple edges between vertices (parallel edges)– Allows self edges (loops)

Figure 14.4Figure 14.4

a) A multigraph is not a graph;

b) a self edge is not allowed in a

graph

Page 8: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -8

Terminology

• Weighted graph– A graph whose edges have numeric labels

Figure 14.5aFigure 14.5a

a) A weighted graph

Page 9: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -9

Terminology

• Undirected graph– Edges do not indicate a direction

• Directed graph, or digraph– Each edge is a directed edge

Figure 14.5bFigure 14.5b

b) A directed graph

Page 10: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -10

Terminology

• Directed graph– Can have two edges between a pair of vertices,

one in each direction– Directed path

• A sequence of directed edges between two vertices

– Vertex y is adjacent to vertex x if• There is a directed edge from x to y

Page 11: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -11

Graphs As ADTs

• Graphs can be used as abstract data types• Two options for defining graphs

– Vertices contain values– Vertices do not contain values

• Operations of the ADT graph– Create an empty graph– Determine whether a graph is empty– Determine the number of vertices in a graph– Determine the number of edges in a graph

Page 12: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -12

Graphs As ADTs

• Operations of the ADT graph (Continued)– Determine whether an edge exists between two given

vertices; for weighted graphs, return weight value– Insert a vertex in a graph whose vertices have distinct

search keys that differ from the new vertex’s search key– Insert an edge between two given vertices in a graph– Delete a particular vertex from a graph and any edges

between the vertex and other vertices– Delete the edge between two given vertices in a graph– Retrieve from a graph the vertex that contains a given

search key

Page 13: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -13

Implementing Graphs

• Most common implementations of a graph– Adjacency matrix– Adjacency list

• Adjacency matrix– Adjacency matrix for a graph with n vertices

numbered 0, 1, …, n – 1• An n by n array matrix such that matrix[i][j] is

– 1 (or true) if there is an edge from vertex i to vertex j– 0 (or false) if there is no edge from vertex i to vertex j

Page 14: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -14

Implementing Graphs

Figure 14.6Figure 14.6

a) A directed graph and b) its adjacency matrix

Page 15: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -15

Implementing Graphs

• Adjacency matrix for a weighted graph with n vertices numbered 0, 1, …, n – 1– An n by n array matrix such that matrix[i][j] is

• The weight that labels the edge from vertex i to vertex j if there is an edge from i to j

if there is no edge from vertex i to vertex j

Figure 14.7Figure 14.7

a) A weighted undirected

graph and b) its

adjacency matrix

Page 16: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -16

Implementing Graphs

• Adjacency list– An adjacency list for a graph with n vertices

numbered 0, 1, …, n – 1• Consists of n linked lists

• The ith linked list has a node for vertex j if and only if the graph contains an edge from vertex i to vertex j

– This node can contain either

» Vertex j’s value, if any

» An indication of vertex j’s identity

Page 17: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -17

Implementing Graphs

Figure 14.8Figure 14.8

a) A directed graph and

b) its adjacency list

Page 18: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -18

Implementing Graphs

• Adjacency list for an undirected graph– Treats each edge as if it were two directed edges in opposite

directions

Figure 14.9Figure 14.9

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

Page 19: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -19

Implementing Graphs

• Adjacency matrix compared with adjacency list– Two common operations on graphs

• Determine whether there is an edge from vertex i to vertex j

• Find all vertices adjacent to a given vertex i

– Adjacency matrix• Supports operation 1 more efficiently

– Adjacency list• Supports operation 2 more efficiently

• Often requires less space than an adjacency matrix

Page 20: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -20

Graph Traversals

• A graph-traversal algorithm– Visits all the vertices that it can reach– Visits all vertices of the graph if and only if the graph is

connected• A connected component

– The subset of vertices visited during a traversal that begins at a given vertex

– Can loop indefinitely if a graph contains a loop• To prevent this, the algorithm must

– Mark each vertex during a visit, and – Never visit a vertex more than once

Page 21: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -21

Graph Traversal

• Two basic graph-traversal algorithms– Depth-first search (DFS) goes as deep into the

graph as it can before backtracking– Breadth-first search (BFS) visits all possible

adjacent vertices before traversing further into the graph

Page 22: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -22

Depth-First Search

• Depth-first search (DFS) traversal– Goes as deeply into the graph as possible from

a vertex before backtracking– A last visited, first explored strategy– A recursive form is simple– An iterative form uses a stack

Page 23: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -23

Graph Traversals

Figure 14.10Figure 14.10

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

Page 24: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -24

Depth-First Search

• Recursive DFS traversaldfs(v)

mark v as visited

for (each unvisited vertex u adjacent to v)

dfs(u)

Page 25: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -25

Depth-First Search• Iterative DFS traversal

dfs(v)s.createStack()s.push(v)mark v as visitedwhile (!s.isEmpty()) {

if (no unvisited vertices adjacent to the vertex on the stack top)

s.pop() // backtrack} else {

select an unvisited vertex u adjacent to the vertex on the

stack tops.push(u)mark u as visited

}}

Page 26: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -26

Depth-First Search

i

ab

f

e

g

c

d

h

Node visited Stack (bottom to top)a ab a bc a b cd a b c dg a b c d ge a b c d g e

Node visited Stack (bottom to top)(cont’d)(backtrack) a b c d gf a b c d g f(backtrack) a b c d g(backtrack) a b c dh a b c d h(backtrack) a b c d(backtrack) a b c(backtrack) a b(backtrack) ai a i(backtrack) a(backtrack) empty

Page 27: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -27

Breadth-First Search

• Breadth-first search (BFS) traversal– Visits every vertex adjacent to a vertex v that it

can before visiting any other vertex– A first visited, first explored strategy– An iterative form uses a queue– A recursive form is possible, but not simple

Page 28: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -28

Breadth-First Search• Iterative BFS traversal

bfs(v)q.createQueue()q.enqueue(v)mark v as visitedwhile (!q.isEmpty()) {

w = q.dequeue()for (each unvisited vertex u adjacent to w) {

mark u as visitedq.enqueue(u)

}}

Page 29: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -29

Breadth-First Search

i

ab

f

e

g

c

d

h

Node visited Queue (front to back)a a

(empty)b bf b fi b f i

f i

Node visited Queue (front to back)(cont’d)c f i ce f i c e

i c eg i c e g

c e ge g

d e g dg dd(empty)

h h(empty)

Page 30: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -30

Applications of Graphs: Topological Sorting

• Topological order– A list of vertices in a directed graph without

cycles such that vertex x precedes vertex y if there is a directed edge from x to y in the graph

– There may be several topological orders in a given graph

• Topological sorting– Arranging the vertices into a topological order

Page 31: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -31

Topological Sorting

Figure 14.15Figure 14.15The graph in Figure 14-14

arranged according to the

topological orders a) a, g, d,

b, e, c, f and b) a, b, g, d, e, f,

c

Figure 14.14Figure 14.14

A directed graph without

cycles

Page 32: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -32

Topological Sorting

• Simple algorithms for finding a topological order– topSort1 (Example: Figure 14-16)

• Find a vertex that has no successor

• Remove from the graph that vertex and all edges that lead to it, and add the vertex to the beginning of a list of vertices

• Add each subsequent vertex that has no successor to the beginning of the list

• When the graph is empty, the list of vertices will be in topological order

Page 33: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -33

Topological Sorting

• Simple algorithms for finding a topological order (Continued)– topSort2 (Example: Figure 14-17)

• A modification of the iterative DFS algorithm

• Strategy– Push all vertices that have no predecessor onto a stack

– Each time you pop a vertex from the stack, add it to the beginning of a list of vertices

– When the traversal ends, the list of vertices will be in topological order

Page 34: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -34

Spanning Trees

• A tree– An undirected connected graph without cycles

• A spanning tree of a connected undirected graph G– A subgraph of G that contains all of G’s

vertices and enough of its edges to form a tree– Example: Figure 14-18

• To obtain a spanning tree from a connected undirected graph with cycles– Remove edges until there are no cycles

Page 35: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -35

Spanning Trees

• You can determine whether a connected graph contains a cycle by counting its vertices and edges– A connected undirected graph that has n vertices must

have at least n – 1 edges

– A connected undirected graph that has n vertices and exactly n – 1 edges cannot contain a cycle

– A connected undirected graph that has n vertices and more than n – 1 edges must contain at least one cycle

Page 36: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -36

Spanning Trees

Figure 14.19Figure 14.19

Connected graphs that each have four vertices and three edges

Page 37: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -37

The DFS Spanning Tree

• To create a depth-first search (DFS) spanning tree– Traverse the graph using a depth-first search

and mark the edges that you follow– After the traversal is complete, the graph’s

vertices and marked edges form the spanning tree

– Example: Figure 14-20

Page 38: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -38

The BFS Spanning Tree

• To create a breath-first search (BFS) spanning tree– Traverse the graph using a breadth-first search

and mark the edges that you follow– When the traversal is complete, the graph’s

vertices and marked edges form the spanning tree

– Example: Figure 14-21

Page 39: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -39

Breadth-First Search

i

ab

f

e

g

c

d

h

Figure 14-20DFS spanning tree

Figure 14-21BFS spanning tree

i

ab

f

e

g

c

d

h

i

ab

f

e

g

c

d

h

Page 40: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -40

Minimum Spanning Tree

• Minimum spanning tree– A spanning tree for which the sum of its edge weights

is minimal

• Prim’s algorithm– Finds a minimal spanning tree that begins at any vertex– Strategy (Example: Figures 14-22 and 14-23)

• Find the least-cost edge (v, u) from a visited vertex v to some unvisited vertex u

• Mark u as visited• Add the vertex u and the edge (v, u) to the minimum spanning

tree• Repeat the above steps until there are no more unvisited

vertices

Page 41: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -41

Minimum Spanning Tree

Figure 14-22A weighted connected, undirected graph

Figure 14-23Prim’s MST algorithm

i

ab

f

e

g

c

d

h2

6

4

79

84

51

3

2

i

ab

f

e

g

c

d

h2

6

4

79

84

51

3

2

Page 42: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -42

Shortest Paths

• Shortest path between two vertices in a weighted graph– The path that has the smallest sum of its edge weights

• Dijkstra’s shortest-path algorithm– Determines the shortest paths between a given origin

and all other vertices– Uses

• A set vertexSet of selected vertices• An array weight, where weight[v] is the weight of the shortest

(cheapest) path from vertex 0 to vertex v that passes through vertices in vertexSet

• weight[u] = min{weight[u], weight[v] + matrix[v][u]}

– Example: Figures 14-24, 14-25, and 14-26

Page 43: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -43

Circuits• A circuit

– A special cycle that passes through every vertex (or edge) in a graph exactly once

• Euler circuit– A circuit that begins at a vertex v, passes through every edge

exactly once, and terminates at v– Exists if and only if each vertex touches an even number of edges

(i.e., has an even degree)– Fig 14-29 and Fig 14-30

Figure 14.27Figure 14.27

a) Euler’s bridge problem

and b) its multigraph

representation

Page 44: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -44

Some Difficult Problems

• Three applications of graphs– The traveling salesperson problem– The three utilities problem– The four-color problem

• A Hamilton circuit– A cycle that begins at a vertex v, passes

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

Page 45: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -45

Hamilton Circuit – Example

b

a

cd

e

f

g

h

i

jkl

m

n

op

q

rs

t

Page 46: Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved 14 -2 Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,

© 2004 Pearson Addison-Wesley. All rights reserved 14 -46

Hamilton Circuit – Example

b

a

cd

e

f

g

h

i

jkl

m

n

op

q

rs

t