0% found this document useful (0 votes)
20 views83 pages

Online Class 19-Graph BFS

Uploaded by

Sumanth K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views83 pages

Online Class 19-Graph BFS

Uploaded by

Sumanth K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 83

GRAPHS

Graphs: Adjacency List


• Adjacency list: for each vertex v  V, store a
list of vertices adjacent to v
• Example: 1
– Adj[1] = {2,3}
– Adj[2] = {3}
2 4
– Adj[3] = {}
– Adj[4] = {3}
3
• Variation: can also keep
a list of edges coming into vertex
David Luebke
2
Graphs: Adjacency List
• How much storage is required?
– The degree of a vertex v = no of incident edges
• Directed graphs have in-degree, out-degree
– For directed graphs, no of of items in adjacency lists is
 out-degree(v) = |E|
takes (V + E) storage (Why?)
– For undirected graphs,no of items in adj lists is
 degree(v) = 2 |E| (handshaking lemma)
also (V + E) storage
• So: Adjacency lists take O(V+E) storage
David Luebke
3
Adjacency lists
Graph implementation
• Array-based implementation
– A 1D array is used to represent the vertices
– A 2D array (adjacency matrix) is used to
represent the edges
Array-based implementation
Linked-list implementation
Edge list
• ..stores the edges and vertices in two lists
• Easy to implement
• Finding the edges incident to a vertex is
inefficient ,since it requires examining the
entire edge sequence.
Edge lists
Breadth-First Search
• “Explore” a graph, turning it into a tree
– One vertex at a time
– Expand frontier of explored vertices across the
breadth of the frontier
• Builds a tree over the graph
– Pick a source vertex to be the root
– Find (“discover”) its children, then their children,
etc.

David Luebke
12
Graph Searching
• Given: a graph G = (V, E), directed or
undirected
• Goal: methodically explore every vertex and
every edge
• Ultimately: build a tree on the graph
– Pick a vertex as the root
– Choose certain edges to produce a tree

David Luebke
13
Breadth-First Search
• BFS follows the following rules:
1. Select an unvisited node x, visit it, have it be the root
in a BFS tree being formed. Its level is called the
current level.
2. From each node z in the current level, in the order in
which the level nodes were visited, visit all the
unvisited neighbors of z. The newly visited nodes from
this level form a new level that becomes the next
current level.
3. Repeat step 2 until no more nodes can be visited.
4. If there are still unvisited nodes, repeat from Step 1.

CS 103 14
BFS and DFS
BFS vs DFS
BFS
• Like unrolling a string ..
Undirected
A
Breadth First Search
H

B C G

D E

18
0Undirected Breadth First Search
A distance from A

visit(A) B C G

D E

F
get

Undiscovered
Fringe Queue: A

Active
Finished 19
Undirected
A
0 Breadth First Search
H

F discovered B C G

D E

1 F

Undiscovered
Fringe Queue:

Active
Finished 20
Undirected
A
0 Breadth First Search
H

B discovered B 1 C G

D E

1 F

Undiscovered
Fringe Queue: F

Active
Finished 21
Undirected
A
0 Breadth First Search
H

C discovered B 1 C 1 G

D E

1 F

Undiscovered
Fringe Queue: F B

Active
Finished 22
Undirected
A
0 Breadth First Search
H
1
G discovered B 1 C 1 G

D E

1 F

Undiscovered
Fringe Queue: F B C

Active
Finished 23
Undirected
A
0 Breadth First Search
H
1
A finished B 1 C 1 G

D E

1 F
get

Undiscovered
Fringe Queue: F B C G

Active
Finished 24
Undirected
A
0 Breadth First Search
H
1
A already
visited B 1 C 1 G

D E

1 F

Undiscovered
Fringe Queue: B C G

Active
Finished 25
Undirected
A
0 Breadth First Search
H
1
D discovered B 1 C 1 G

2 D E

1 F

Undiscovered
Fringe Queue: B C G

Active
Finished 26
Undirected
A
0 Breadth First Search
H
1
E discovered B 1 C 1 G

2
2 D E

1 F

Undiscovered
Fringe Queue: B C G D

Active
Finished 27
Undirected
A
0 Breadth First Search
H
1
F finished B 1 C 1 G

2
2 D E

1 F
get

Undiscovered
Fringe Queue: B C G D E

Active
Finished 28
Undirected
A
0 Breadth First Search
H
1
B 1 C 1 G

2
2 D E

1 F

Undiscovered
Fringe Queue: C G D E

Active
Finished 29
Undirected
A
0 Breadth First Search
H
1
A already
visited B 1 C 1 G

2
2 D E

1 F

Undiscovered
Fringe Queue: C G D E

Active
Finished 30
Undirected
A
0 Breadth First Search
H
1
B finished B 1 C 1 G

2
2 D E

1 F
get

Undiscovered
Fringe Queue: C G D E

Active
Finished 31
Undirected
A
0 Breadth First Search
H
1
A already
visited B 1 C 1 G

2
2 D E

1 F

Undiscovered
Fringe Queue: G D E

Active
Finished 32
Undirected
A
0 Breadth First Search
H
1
C finished B 1 C 1 G

2
2 D E

1 F
get

Undiscovered
Fringe Queue: G D E

Active
Finished 33
Undirected
A
0 Breadth First Search
H
1
A already
visited B 1 C 1 G

2
2 D E

1 F

Undiscovered
Fringe Queue: D E

Active
Finished 34
Undirected
A
0 Breadth First Search
H
1
E already
visited B 1 C 1 G

2
2 D E

1 F

Undiscovered
Fringe Queue: D E

Active
Finished 35
Undirected
A
0 Breadth First Search
H
1
G finished B 1 C 1 G

2
2 D E

1 F
get

Undiscovered
Fringe Queue: D E

Active
Finished 36
Undirected
A
0 Breadth First Search
H
1
E already
visited B 1 C 1 G

2
2 D E

1 F

Undiscovered
Fringe Queue: E

Active
Finished 37
Undirected
A
0 Breadth First Search
H
1
F already
visited B 1 C 1 G

2
2 D E

1 F

Undiscovered
Fringe Queue: E

Active
Finished 38
Undirected
A
0 Breadth First Search
H
1
D finished B 1 C 1 G

2
2 D E

1 F
get

Undiscovered
Fringe Queue: E

Active
Finished 39
Undirected
A
0 Breadth First Search
H
1
D already
visited B 1 C 1 G

2
2 D E

1 F

Undiscovered
Fringe Queue:

Active
Finished 40
Undirected
A
0 Breadth First Search
H
1
F already
visited B 1 C 1 G

2
2 D E

1 F

Undiscovered
Fringe Queue:

Active
Finished 41
Undirected
A
0 Breadth First Search
H
1
G already
visited B 1 C 1 G

2
2 D E

1 F

Undiscovered
Fringe Queue:

Active
Finished 42
Undirected
A
0 Breadth First Search
H3
1
H discovered B 1 C 1 G

2
2 D E

1 F

Undiscovered
Fringe Queue:

Active
Finished 43
Undirected
A
0 Breadth First Search
H3
1
E finished B 1 C 1 G

2
2 D E

1 F
get

Undiscovered
Fringe Queue: H

Active
Finished 44
Undirected
A
0 Breadth First Search
H3
1
E already
visited B 1 C 1 G

2
2 D E

1 F

Undiscovered
Fringe Queue:

Active
Finished 45
Undirected
A
0 Breadth First Search
H3
1
H finished B 1 C 1 G

2
2 D E

1 F
STOP

Undiscovered
Fringe Queue:

Active
Finished 46
Undirected
A
0 Breadth First Search
distance from A

H3
1
B 1 C 1 G

2
2 D E

1 F

47
BFS –pseudo code and analysis
• Given a graph G=(V,E) , and a source vertex
s ,BFS…
• systematically explores the graph to discover
every vertex that is reachable from s
• It computes the distance (smallest no of
edges) from s to each reachable vertex
• Works on directed and undirected graphs
• To keep track of progress, breadth-first-search
colors each vertex. Each vertex of the graph is
in one of three states:
• 1. Undiscovered;
2. Discovered but not fully explored; and
3. Fully explored.
• The state of a vertex, u, is stored in a color
variable as follows:
• 1. color[u] = White - for the "undiscovered"
state,
2. color [u] = Gray - for the "discovered but not
fully explored" state, and
3. color [u] = Black - for the "fully explored"
state.
To keep track of discovered vertices ..colors
are used
• All vertices start out white and then become gray
and then black
• A discovered vertex becomes gray
• When the search pertaining to a vertex is ’done
with’ ,it is made black
• The BFS tree maintains the parent ,child ,ancestor
…etc …relationship as in any tree..
• Since a vertex is discovered only once, it has only
one parent ..
• color of each vertex-u.color
• predecessor of u =u.pi
• When u has no predecessor, u.pi=NIL
• u.d holds the distance from the source
s to vertex u
• lines 1–4 paint every vertex white,
• And sets u.d=infinity ,u.pi =NIL
• Line 5 paints s gray Line 6 initializes s.d
to 0, and line 7 sets the predecessor to
NIL
• Lines 8–9 initialize Q to the queue
containing just the vertex s
• lines 10–18 iterates as long as there
remain gray vertices
• At test in line 10, the queue Q consists
of the set of gray vertices.
Analysis-Time
• Enqueuing and dequeuing takes O(1)
• Total queue operations are limited to O(V)
• The procedure scans the adjacency list only once for
each vertex (when the vertex is dequeued)
• The sum of the lengths of the adjacency lists is
Theta(E)
• The total time spent in scanning adjacency lists is
O(E).
• Initialiszation takes O(V)
• Running time of BFS is O(V+E)
Breadth First Search
2 4 8

s 5 7

3 6 9

55
Shortest path Breadth First Search
1
from s
2 4 8

0 s 5 7

3 6 9

Undiscovered
Discovered Queue: s

Top of queue
Finished 56
Breadth First Search
1
2 4 8

0 s 5 7

3 6 9

Undiscovered
Discovered Queue: s 2

Top of queue
Finished 57
Breadth First Search
1
2 4 8

0 s 5 7
1

3 6 9

Undiscovered
Discovered Queue: s 2 3

Top of queue
Finished 58
Breadth First Search
1
2 4 8

0 s 5 7
1

3 6 9

Undiscovered
Discovered Queue: 2 3 5

Top of queue
Finished 59
Breadth First Search
1 2
2 4 8

0 s 5 7
1

3 6 9

Undiscovered
Discovered Queue: 2 3 5

Top of queue
Finished 60
Breadth First Search
1 2
2 4 8

5 already discovered:
0 s 5 7
don't enqueue
1

3 6 9

Undiscovered
Discovered Queue: 2 3 5 4

Top of queue
Finished 61
Breadth First Search
1 2
2 4 8

0 s 5 7
1

3 6 9

Undiscovered
Discovered Queue: 2 3 5 4

Top of queue
Finished 62
Breadth First Search
1 2
2 4 8

0 s 5 7
1

3 6 9

Undiscovered
Discovered Queue: 3 5 4

Top of queue
Finished 63
Breadth First Search
1 2
2 4 8

0 s 5 7
1

3 6 9

1 2

Undiscovered
Discovered Queue: 3 5 4

Top of queue
Finished 64
Breadth First Search
1 2
2 4 8

0 s 5 7
1

3 6 9

1 2

Undiscovered
Discovered Queue: 3 5 4 6

Top of queue
Finished 65
Breadth First Search
1 2
2 4 8

0 s 5 7
1

3 6 9

1 2

Undiscovered
Discovered Queue: 5 4 6

Top of queue
Finished 66
Breadth First Search
1 2
2 4 8

0 s 5 7
1

3 6 9

1 2

Undiscovered
Discovered Queue: 5 4 6

Top of queue
Finished 67
Breadth First Search
1 2
2 4 8

0 s 5 7
1

3 6 9

1 2

Undiscovered
Discovered Queue: 4 6

Top of queue
Finished 68
Breadth First Search
1 2 3
2 4 8

0 s 5 7
1

3 6 9

1 2

Undiscovered
Discovered Queue: 4 6

Top of queue
Finished 69
Breadth First Search
1 2 3
2 4 8

0 s 5 7
1

3 6 9

1 2

Undiscovered
Discovered Queue: 4 6 8

Top of queue
Finished 70
Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2

Undiscovered
Discovered Queue: 6 8

Top of queue
Finished 71
Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered Queue: 6 8 7

Top of queue
Finished 72
Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered Queue: 6 8 7 9

Top of queue
Finished 73
Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered Queue: 8 7 9

Top of queue
Finished 74
Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered Queue: 7 9

Top of queue
Finished 75
Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered Queue: 7 9

Top of queue
Finished 76
Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered Queue: 7 9

Top of queue
Finished 77
Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered Queue: 7 9

Top of queue
Finished 78
Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered Queue: 9

Top of queue
Finished 79
Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered Queue: 9

Top of queue
Finished 80
Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered Queue: 9

Top of queue
Finished 81
Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered Queue:

Top of queue
Finished 82
Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Level Graph

83

You might also like