Graphs I - PowerPoint PPT Presentation

About This Presentation
Title:

Graphs I

Description:

Graphs I Kruse and Ryba Chapter 12 – PowerPoint PPT presentation

Number of Views:63
Avg rating:3.0/5.0
Slides: 110
Provided by: StanSc7
Learn more at: https://www.cs.bu.edu
Category:
Tags: graphs

less

Transcript and Presenter's Notes

Title: Graphs I


1
Graphs I
  • Kruse and Ryba
  • Chapter 12

2
Undirected Graphs
  • An undirected graph is a set of nodes and a set
    of edges between nodes e.g.
  • The graph is undirected because its edges do not
    have a particular direction.

3
Edges
  • We sometimes need to refer to the edges ei of a
    graph

e0
e5
e1
e2
e4
e3
4
Vertices
  • We also need to refer to the vertices vj of a
    graph

e0
e5
v0
v4
v1
e1
e2
e4
v2
e3
v3
5
Undirected Graph Definition
  • An undirected graph G (V, E) is a finite set of
    vertices V, together with a finite set of edges
    E.
  • Both sets might be empty (no vertices and no
    edges), which is called the empty graph.
  • Each edge has associated with it two vertices.
  • If two vertices are connected by an edge, they
    are said to be adjacent.

6
Undirected Graph Example Subway Map
7
Another Undirected Graph vBNS (fast internet
backbone)
8
Directed Graphs
  • In a directed graph each vertex has an associated
    direction e.g.
  • Each edge is associated with two vertices its
    source vertex and target vertex.

9
Directed Graph Examples
  • Flow graph vertices are pumping stations and
    edges are pipelines connecting them.
  • Task network each project has a number of
    component activities called tasks. Each task has
    a duration (amount of time needed to complete
    task).

10
Loops
  • A loop is an edge that connects a vertex with
    itself for example
  • Loops can occur in directed graphs or undirected
    graphs.

11
Multiple Edges
  • In principle, a graph may have two or more edges
    connecting the same two vertices in the same
    direction
  • These are called multiple edges. In a graph
    diagram, each edge is drawn separately.

12
Simple Graphs
  • A graph is said to be a simple graph, if
  • the graph has no loops, and
  • no multiple edges

13
Path
  • A path in a graph is a sequence of vertices, v0,
    v1, vm, such that each adjacent pair of
    vertices in the list are connected by an edge.

v0
v4
v1
v2
v3
14
Path
  • A path in a graph is a sequence of vertices, v0,
    v1, vm, such that each adjacent pair of
    vertices in the list are connected by an edge.

Example path v0, v3, v4
v0
v4
v1
v2
v3
15
Path
  • A path in a graph is a sequence of vertices, v0,
    v1, vm, such that each adjacent pair of
    vertices in the list are connected by an edge.

Example path v0, v3, v4 v0, v3, v2, v1, v4
v0
v4
v1
v2
v3
16
Path
  • In a directed graph, the connection must go from
    source vi to target vi1.

Is there a path from v0 to v4? v3 to v1? v1 to
v2? v2 to v1?
v0
v4
v1
v2
v3
17
Example Task Network
Investigate neighborhood schools
Find a realtor
Look at homes
3
Investigate termite and soil reports
5
9
Start
Present offer
Choose a home to buy
10
2
Apply for financing and establish price limit
0
2
Prepare offer
15
20
How long will it take to buy a house? (The
duration of each task is shown below each task).
18
Critical Path
Investigate neighborhood schools
Find a realtor
Look at homes
3
Investigate termite and soil reports
5
9
Start
Present offer
Choose a home to buy
10
2
Apply for financing and establish price limit
0
2
Prepare offer
15
20
The task will take as long as the maximum
duration path from start to finish.
19
Cycles
  • A cycle in a graph is a path that leads back to
    the first node in the path.

Example cycles v1, v2, v3, v2, v1
v0
v4
v1
v2
v3
20
Cycles
  • A cycle in a graph is a path that leads back to
    the first node in the path.

Example cycles v1, v2, v3, v2, v1 v0, v3, v4,
v1, v0
v0
v4
v1
v2
v3
21
Does this graph have any cycles?
22
Cycles in Directed Graphs
  • A cycle in a directed graph must follow edge
    directions e.g.

23
DAG Directed Acyclic Graph
  • A directed acyclic graph (DAG) must have no
    cycles e.g.
  • In other words, there is no path that starts and
    ends at the same node.

24
Complete Graph
  • An undirected graph which has an edge between
    every pair of vertices.

25
Connected Graph
  • An undirected graph which has a path between
    every pair of vertices.

1
2
3
0
26
Strongly Connected Graph
  • A directed graph which has a path between every
    pair of vertices.

27
Weakly Connected Graph
  • A directed graph which is connected, but not not
    strongly connected.

28
A DAG that is not Connected
29
Connected Components
30
Review of Terms
  • Undirected vs. directed graphs
  • Multiple edges, loops
  • Simple graph
  • Path
  • Cycle
  • Directed, acyclic graph (DAG)
  • Connected, strongly connected, and weakly
    connected graphs
  • Connected component

31
Graph Implementations
32
Adjacency Matrix
0 1 2 3
0 1 2 3




2
0
1
3
33
Adjacency Matrix
0 1 2 3
0 1 2 3
1



2
0
1
3
34
Adjacency Matrix
0 1 2 3
0 1 2 3
1 0



2
0
1
3
35
Adjacency Matrix
0 1 2 3
0 1 2 3
1 0 1



2
0
1
3
36
Adjacency Matrix
0 1 2 3
0 1 2 3
1 0 1 1



2
0
1
3
37
Adjacency Matrix
0 1 2 3
0 1 2 3
1 0 1 1
0 0 0 1


2
0
1
3
38
Adjacency Matrix
0 1 2 3
0 1 2 3
1 0 1 1
0 0 0 1
1 0 0 0

2
0
1
3
39
Adjacency Matrix
0 1 2 3
0 1 2 3
1 0 1 1
0 0 0 1
1 0 0 0
0 1 0 0
2
0
1
3
40
C Adjacency Matrix
0 1 2 3
0 1 2 3
1 0 1 1
0 0 0 1
1 0 0 0
0 1 0 0
2
0
1
3
bool graph_adjacency44
41
Adjacency Matrix Weighted Edges
6.0
0 1 2 3
2.5
2
5.8 0 6.0 1.5
0 0 0 2.5
2.5 0 0 0
0 4.2 0 0
0 1 2 3
5.8
0
1
2.5
1.5
3
4.2
double graph_adjacency44
42
Weighted Edges An Example
Flights. Whats the cheapest way to fly San
Fransisco ? Boston?
Boston
San Fransisco
452
245
320
Chicago
60
180
Dallas
43
Weighted Edges An Example
Mileage for various hops from San Fransisco ?
Boston
Boston
San Fransisco
2800
1200
1800
Chicago
2300
1600
Dallas
Mileage is not real, but for sake of example.
44
Weighted Edges An Example
Available internet bandwidth on various paths
between an ADSL provider and customers.
Pat
Fran
100Kbs
1Gbs
500Kbs
100Kbs
ADSL provider
This is a simplication for sake of example,
and bit rates are fictional.
45
Adjacency Matrix Undirected Graph
0 1 2 3
0 1 2 3




2
0
1
3
bool graph_adjacency44
46
Adjacency Matrix Undirected Graph
0 1 2 3
0 1 2 3
0
0
0
0
2
0
1
3
No loops ? zero diagonal.
47
Adjacency Matrix Undirected Graph
0 1 2 3
0 1 2 3
0 1 1 1
0
0
0
2
0
1
3
48
Adjacency Matrix Undirected Graph
0 1 2 3
0 1 2 3
0 1 1 1
1 0 0 1
0
0
2
0
1
3
49
Adjacency Matrix Undirected Graph
0 1 2 3
0 1 2 3
0 1 1 1
1 0 0 1
1 0 0 0
0
2
0
1
3
50
Adjacency Matrix Undirected Graph
0 1 2 3
0 1 2 3
0 1 1 1
1 0 0 1
1 0 0 0
1 1 0 0
2
0
1
3
51
Symmetry of Adjacency Matrix of Undirected Graph
0 1 2 3
0 1 2 3
0 1 1 1
1 0 0 1
1 0 0 0
1 1 0 0
2
0
1
3
aij aji
52
Non-symmetric Adjacency Matrix for Directed Graph
0 1 2 3
0 1 2 3
1 0 1 1
0 0 0 1
1 0 0 0
0 1 0 0
2
0
1
3
aij? aji
53
Symmetric Adjacency Matrix
  • An undirected graph will always have a symmetric
    adjacency matrix.
  • A directed graph may not have a symmetric
    adjacency matrix.
  • If a directed graph has a symmetric matrix, what
    does that mean?

54
Non-symmetric Adjacency Matrix Unweighted
Directed Graph
0 1 2 3
0 1 2 3
1 0 1 1
0 0 0 1
1 0 0 0
0 1 0 0
2
0
1
3
aij? aji
55
Add an Edge
0 1 2 3
0 1 2 3
1 0 1 1
0 0 0 1
1 0 0 0
1 1 0 0
2
0
1
3
aijaji
56
Adjacency Matrix for Weighted Directed Graph
3
0 1 2 3
0 1 2 3
5 0 3 6
0 0 0 4
3 0 0 0
6 4 0 0
2
3
2
0
5
1
6
4
3
6
4
aijaji
57
Time Complexity Analysis
  • Given N we have vertices in a graph, and we use
    the adjacency matrix representation.
  • What is the worst case and average complexity of
    inserting an edge in the graph?
  • What is the worst case and average complexity of
    removing an edge in the graph?
  • What is the complexity of retrieving a list of
    all nodes that share and edge with a particular
    vertex?

58
Representing Graphs with Edge Lists
2
Edge list for vertex 0
Edge list for vertex 1
Edge list for vertex 2
Edge list for vertex 3
0
1
Null
2
0
1
3
Null
Null
3
Null
59
Time Complexity Analysis
  • Given N we have vertices in a graph, and we use
    the edge list representation.
  • What is the worst case and average complexity of
    inserting an edge in the graph?
  • What is the worst case and average complexity of
    removing an edge in the graph?
  • What is the complexity of retrieving a list of
    all nodes that share and edge with a particular
    vertex?

60
Representing Graphs with Edge Sets
Assume a Set template class is available Declare
array of sets, one per node in graph Setltintgt
connectionsN Each nodes set contains the
indices of all vertices that it is connected to.
2
0
1
3
61
Time Complexity Analysis
  • Given N we have vertices in a graph, and we use
    the edge set representation.
  • What is the worst case and average complexity of
    inserting an edge in the graph?
  • What is the worst case and average complexity of
    removing an edge in the graph?
  • What is the complexity of retrieving a list of
    all nodes that share and edge with a particular
    vertex?

62
Graph Implementations Summary
  • Adjacency matrix
  • Edge lists
  • Edge sets

63
Graph Searching
64
Graph Search
  • Choice of container
  • If a stack is used as the container for adjacent
    vertices, we get depth first search.
  • If a list is used as the container adjacent
    vertices, we get breadth first search.

65
Breadth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a queue Q
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • Q? s
  • While Q ? ?
  • u ? headQ
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Enqueue(Q,v)
  • Dequeue(Q)
  • coloru ? black

1
4
0
2
5
3
66
Breadth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a queue Q
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • Q? s
  • While Q ? ?
  • u ? headQ
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Enqueue(Q,v)
  • Dequeue(Q)
  • coloru ? black

1
4
s
0
2
5
3
Q ?
67
Breadth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a queue Q
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • Q? s
  • While Q ? ?
  • u ? headQ
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Enqueue(Q,v)
  • Dequeue(Q)
  • coloru ? black

1
4
s
0
2
5
3
Q ?
68
Breadth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a queue Q
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • Q? s
  • While Q ? ?
  • u ? headQ
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Enqueue(Q,v)
  • Dequeue(Q)
  • coloru ? black

1
4
s
0
2
5
3
Q
0
69
Breadth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a queue Q
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • Q? s
  • While Q ? ?
  • u ? headQ
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Enqueue(Q,v)
  • Dequeue(Q)
  • coloru ? black

1
4
0
2
5
3
u
Q
0
70
Breadth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a queue Q
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • Q? s
  • While Q ? ?
  • u ? headQ
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Enqueue(Q,v)
  • Dequeue(Q)
  • coloru ? black

1
4
0
2
5
3
u
Q
0
1
71
Breadth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a queue Q
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • Q? s
  • While Q ? ?
  • u ? headQ
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Enqueue(Q,v)
  • Dequeue(Q)
  • coloru ? black

1
4
0
2
5
3
u
Q
0
1
2
72
Breadth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a queue Q
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • Q? s
  • While Q ? ?
  • u ? headQ
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Enqueue(Q,v)
  • Dequeue(Q)
  • coloru ? black

1
4
0
2
5
3
u
Q
0
1
2
3
73
Breadth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a queue Q
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • Q? s
  • While Q ? ?
  • u ? headQ
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Enqueue(Q,v)
  • Dequeue(Q)
  • coloru ? black

1
4
0
2
5
3
u
Q
0
1
2
3
74
Breadth First Algorithm
u
  • Given graph G(V,E) and source vertex s ? V
  • Create a queue Q
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • Q? s
  • While Q ? ?
  • u ? headQ
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Enqueue(Q,v)
  • Dequeue(Q)
  • coloru ? black

1
4
0
2
5
3
Q
0
1
2
3
75
Breadth First Algorithm
u
  • Given graph G(V,E) and source vertex s ? V
  • Create a queue Q
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • Q? s
  • While Q ? ?
  • u ? headQ
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Enqueue(Q,v)
  • Dequeue(Q)
  • coloru ? black

1
4
0
2
5
3
Q
0
1
2
3
4
76
Breadth First Algorithm
u
  • Given graph G(V,E) and source vertex s ? V
  • Create a queue Q
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • Q? s
  • While Q ? ?
  • u ? headQ
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Enqueue(Q,v)
  • Dequeue(Q)
  • coloru ? black

1
4
0
2
5
3
Q
0
1
2
3
4
77
Breadth First Algorithm
u
  • Given graph G(V,E) and source vertex s ? V
  • Create a queue Q
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • Q? s
  • While Q ? ?
  • u ? headQ
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Enqueue(Q,v)
  • Dequeue(Q)
  • coloru ? black

1
4
0
2
5
3
Q
0
1
2
3
4
78
Breadth First Algorithm
u
  • Given graph G(V,E) and source vertex s ? V
  • Create a queue Q
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • Q? s
  • While Q ? ?
  • u ? headQ
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Enqueue(Q,v)
  • Dequeue(Q)
  • coloru ? black

1
4
0
2
5
3
Q
0
1
2
3
4
79
Breadth First Algorithm
u
  • Given graph G(V,E) and source vertex s ? V
  • Create a queue Q
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • Q? s
  • While Q ? ?
  • u ? headQ
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Enqueue(Q,v)
  • Dequeue(Q)
  • coloru ? black

1
4
0
2
5
3
Q
0
1
2
3
4
80
Breadth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a queue Q
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • Q? s
  • While Q ? ?
  • u ? headQ
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Enqueue(Q,v)
  • Dequeue(Q)
  • coloru ? black

1
4
0
2
5
3
u
Q
0
1
2
3
4
81
Breadth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a queue Q
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • Q? s
  • While Q ? ?
  • u ? headQ
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Enqueue(Q,v)
  • Dequeue(Q)
  • coloru ? black

1
4
0
2
5
3
u
Q
0
1
2
3
4
5
82
Breadth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a queue Q
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • Q? s
  • While Q ? ?
  • u ? headQ
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Enqueue(Q,v)
  • Dequeue(Q)
  • coloru ? black

1
4
0
2
5
3
u
Q
0
1
2
3
4
5
83
Breadth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a queue Q
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • Q? s
  • While Q ? ?
  • u ? headQ
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Enqueue(Q,v)
  • Dequeue(Q)
  • coloru ? black

1
4
0
2
5
3
u
Q
0
1
2
3
4
5
84
Breadth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a queue Q
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • Q? s
  • While Q ? ?
  • u ? headQ
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Enqueue(Q,v)
  • Dequeue(Q)
  • coloru ? black

1
4
0
2
5
3
u
Q
0
1
2
3
4
5
85
Breadth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a queue Q
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • Q? s
  • While Q ? ?
  • u ? headQ
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Enqueue(Q,v)
  • Dequeue(Q)
  • coloru ? black

1
4
0
2
5
3
Q
u
0
1
2
3
4
5
86
Breadth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a queue Q
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • Q? s
  • While Q ? ?
  • u ? headQ
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Enqueue(Q,v)
  • Dequeue(Q)
  • coloru ? black

1
4
0
2
5
3
Q ?
u
87
Graph Search
  • Choice of container
  • If a stack is used as the container for adjacent
    vertices, we get depth first search.
  • If a list is used as the container adjacent
    vertices, we get breadth first search.

88
Depth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a stack S
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • S? s
  • While S ? ?
  • u Pop(S)
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Push(S,v)
  • coloru ? black

1
4
s
0
2
5
3
S ?
89
Depth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a stack S
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • S? s
  • While S ? ?
  • u Pop(S)
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Push(S,v)
  • coloru ? black

1
4
s
0
2
5
3
S ?
90
Depth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a stack S
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • S? s
  • While S ? ?
  • u Pop(S)
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Push(S,v)
  • coloru ? black

1
4
s
0
2
5
3
S ?
0
91
Depth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a stack S
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • S? s
  • While S ? ?
  • u Pop(S)
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Push(S,v)
  • coloru ? black

1
4
0
2
5
u
3
S ?
92
Depth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a stack S
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • S? s
  • While S ? ?
  • u Pop(S)
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Push(S,v)
  • coloru ? black

1
4
0
2
5
u
3
S ?
1
93
Depth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a stack S
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • S? s
  • While S ? ?
  • u Pop(S)
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Push(S,v)
  • coloru ? black

1
4
0
2
5
u
3
S ?
3
2
1
94
Depth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a stack S
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • S? s
  • While S ? ?
  • u Pop(S)
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Push(S,v)
  • coloru ? black

1
4
0
2
5
u
3
S ?
3
2
1
95
Depth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a stack S
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • S? s
  • While S ? ?
  • u Pop(S)
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Push(S,v)
  • coloru ? black

1
4
0
2
5
u
3
S ?
3
2
1
96
Depth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a stack S
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • S? s
  • While S ? ?
  • u Pop(S)
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Push(S,v)
  • coloru ? black

1
4
0
2
5
3
u
S ?
3
2
1
97
Depth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a stack S
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • S? s
  • While S ? ?
  • u Pop(S)
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Push(S,v)
  • coloru ? black

1
4
0
2
5
3
u
S ?
3
2
1
5
98
Depth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a stack S
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • S? s
  • While S ? ?
  • u Pop(S)
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Push(S,v)
  • coloru ? black

1
4
0
2
5
3
u
S ?
3
2
1
5
99
Depth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a stack S
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • S? s
  • While S ? ?
  • u Pop(S)
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Push(S,v)
  • coloru ? black

1
4
0
2
5
3
u
S ?
3
5
2
1
100
Depth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a stack S
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • S? s
  • While S ? ?
  • u Pop(S)
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Push(S,v)
  • coloru ? black

1
4
0
2
5
3
u
S ?
3
5
2
1
4
101
Depth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a stack S
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • S? s
  • While S ? ?
  • u Pop(S)
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Push(S,v)
  • coloru ? black

1
4
0
2
5
3
u
S ?
3
5
2
1
4
102
Depth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a stack S
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • S? s
  • While S ? ?
  • u Pop(S)
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Push(S,v)
  • coloru ? black

1
4
0
2
u
5
3
S ?
4
3
5
2
1
103
Depth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a stack S
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • S? s
  • While S ? ?
  • u Pop(S)
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Push(S,v)
  • coloru ? black

1
4
0
2
u
5
3
S ?
4
3
5
2
1
104
Depth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a stack S
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • S? s
  • While S ? ?
  • u Pop(S)
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Push(S,v)
  • coloru ? black

1
4
0
2
u
5
3
S ?
4
3
5
2
1
105
Depth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a stack S
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • S? s
  • While S ? ?
  • u Pop(S)
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Push(S,v)
  • coloru ? black

1
4
0
2
u
5
3
S ?
4
3
5
2
1
106
Depth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a stack S
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • S? s
  • While S ? ?
  • u Pop(S)
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Push(S,v)
  • coloru ? black

1
4
0
2
u
5
3
S ?
107
Depth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a stack S
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • S? s
  • While S ? ?
  • u Pop(S)
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Push(S,v)
  • coloru ? black

1
4
0
2
u
5
3
S ?
108
Graph Search
  • Choice of container
  • If a stack is used as the container for adjacent
    vertices, we get depth first search.
  • If a list is used as the container adjacent
    vertices, we get breadth first search.

109
Depth First Algorithm
  • Given graph G(V,E) and source vertex s ? V
  • Create a stack S
  • For each vertex u ? V s
  • coloru ? white
  • colors ? gray
  • S? s
  • While S ? ?
  • u Pop(S)
  • for each v ? Adjacentu
  • if colorv white
  • colorv ? gray
  • Push(S,v)
  • coloru ? black

1
4
0
2
u
5
3
S ?
Write a Comment
User Comments (0)
About PowerShow.com