SlideShare a Scribd company logo
1 of 41
Graph & BFS
1
Graph & BFS / Slide 2
Graphs
Extremely useful tool in modeling problems
Consist of:
Vertices
Edges D
E
A
C
F
B
Vertex
Edge
Vertices can be
considered “sites”
or locations.
Edges represent
connections.
2
Graph & BFS / Slide 3
Application
Air flight system
• Each vertex represents a city
• Each edge represents a direct flight between two cities
• A query on direct flights = a query on whether an edge exists
• A query on how to get to a location = does a path exist from A to B
• We can even associate costs to edges (weighted graphs), then
ask “what is the cheapest path from A to B”
3
Graph & BFS / Slide 4
Definition
A graph G=(V, E) consists a set of vertices, V, and a set
of edges, E.
Each edge is a pair of (v, w), where v, w belongs to V
If the pair is unordered, the graph is undirected;
otherwise it is directed
{c,f}
{a,c}{a,b}
{b,d} {c,d}
{e,f}
{b,e}
An undirected graph
4
Graph & BFS / Slide 5
Definition
Complete Graph
How many edges are there in an N-vertex
complete graph?
Bipartite Graph
What is its property? How can we detect it?
Path
Tour
Degree of a vertices
Indegree
Outdegree
Indegree+outdegree = Even (why??) 5
Graph & BFS / Slide 6
Graph Variations
Variations:
A connected graph has a path from every
vertex to every other
In an undirected graph:
 Edge (u,v) = Edge (v,u)
 No self-loops
In a directed graph:
 Edge (u,v) goes from vertex u to vertex v, notated u→v
6
Graph & BFS / Slide 7
Graph Variations
More variations:
A weighted graph associates weights with
either the edges or the vertices
 E.g., a road map: edges might be weighted w distance
A multi-graph allows multiple edges
between the same vertices
 E.g., the call graph in a program (a function can get called
from multiple points in another function)
7
Graph & BFS / Slide 8
Graphs
We will typically express running times
in terms of |E| and |V| (often dropping
the |’s)
If |E| ≈ |V|2
the graph is dense
If |E| ≈ |V| the graph is sparse
If you know you are dealing with dense
or sparse graphs, different data
structures may make sense
8
Graph & BFS / Slide 9
Graph Representation
Two popular computer representations of
a graph. Both represent the vertex set
and the edge set, but in different ways.
1. Adjacency Matrix
Use a 2D matrix to represent the graph
1. Adjacency List
Use a 1D array of linked lists
9
Graph & BFS / Slide 10
Adjacency Matrix
2D array A[0..n-1, 0..n-1], where n is the number of vertices in
the graph
Each row and column is indexed by the vertex id
e,g a=0, b=1, c=2, d=3, e=4
A[i][j]=1 if there is an edge connecting vertices i and j; otherwise,
A[i][j]=0
The storage requirement is Θ(n2
). It is not efficient if the graph
has few edges. An adjacency matrix is an appropriate
representation if the graph is dense: |E|=Θ(|V|2
)
We can detect in O(1) time whether two vertices are connected. 10
Graph & BFS / Slide 11
Simple Questions on Adjacency Matrix
Is there a direct link between A and B?
What is the indegree and outdegree for
a vertex A?
How many nodes are directly connected
to vertex A?
Is it an undirected graph or directed
graph?
Suppose ADJ is an NxN matrix. What
will be the result if we create another
matrix ADJ2 where ADJ2=ADJxADJ?
11
Graph & BFS / Slide 12
Adjacency List
If the graph is not dense, in other words, sparse, a
better solution is an adjacency list
The adjacency list is an array A[0..n-1] of lists, where
n is the number of vertices in the graph.
Each array entry is indexed by the vertex id
Each list A[i] stores the ids of the vertices adjacent to
vertex i 12
Graph & BFS / Slide 13
Adjacency Matrix Example
2
4
3
5
1
7
6
9
8
0 0 1 2 3 4 5 6 7 8 9
0 0 0 0 0 0 0 0 0 1 0
1 0 0 1 1 0 0 0 1 0 1
2 0 1 0 0 1 0 0 0 1 0
3 0 1 0 0 1 1 0 0 0 0
4 0 0 1 1 0 0 0 0 0 0
5 0 0 0 1 0 0 1 0 0 0
6 0 0 0 0 0 1 0 1 0 0
7 0 1 0 0 0 0 1 0 0 0
8 1 0 1 0 0 0 0 0 0 1
9 0 1 0 0 0 0 0 0 1 0
13
Graph & BFS / Slide 14
Adjacency List Example
2
4
3
5
1
7
6
9
8
0 0
1
2
3
4
5
6
7
8
9
2 3 7 9
8
1 4 8
1 4 5
2 3
3 6
5 7
1 6
0 2 9
1 8
14
Graph & BFS / Slide 15
The array takes up Θ(n) space
Define degree of v, deg(v), to be the number of edges incident to
v. Then, the total space to store the graph is proportional to:
An edge e={u,v} of the graph contributes a count of 1 to deg(u)
and contributes a count 1 to deg(v)
Therefore, Σvertex vdeg(v) = 2m, where m is the total number of
edges
In all, the adjacency list takes up Θ(n+m) space
If m = O(n2
) (i.e. dense graphs), both adjacent matrix and adjacent
lists use Θ(n2
) space.
If m = O(n), adjacent list outperform adjacent matrix
However, one cannot tell in O(1) time whether two vertices are
connected
Storage of Adjacency List
∑v
v
vertex
)deg(
15
Graph & BFS / Slide 16
Adjacency List vs. Matrix
Adjacency List
More compact than adjacency matrices if graph has few
edges
Requires more time to find if an edge exists
Adjacency Matrix
Always require n2
space
 This can waste a lot of space if the number of edges are sparse
Can quickly find if an edge exists
16
Graph & BFS / Slide 17
Path between Vertices
A path is a sequence of vertices (v0, v1,
v2,… vk) such that:
For 0 ≤ i < k, {vi, vi+1} is an edge
Note: a path is allowed to go through the same vertex or the
same edge any number of times!
The length of a path is the number of
edges on the path
17
Graph & BFS / Slide 18
Types of paths
A path is simple if and only if it does
not contain a vertex more than
once.
A path is a cycle if and only if v0= vk
 The beginning and end are the same vertex!
A path contains a cycle as its sub-path if
some vertex appears twice or more
18
Graph & BFS / Slide 19
Path Examples
1. {a,c,f,e}
2. {a,b,d,c,f,e}
3. {a, c, d, b, d, c, f, e}
4. {a,c,d,b,a}
5. {a,c,f,e,b,d,c,a}
Are these paths?
Any cycles?
What is the path’s length?
19
Graph & BFS / Slide 20
Graph Traversal
Application example
Given a graph representation and a vertex s
in the graph
Find paths from s to other vertices
Two common graph traversal algorithms
 Breadth-First Search (BFS)
Find the shortest paths in an unweighted graph
 Depth-First Search (DFS)
Topological sort
Find strongly connected components
20
Graph & BFS / Slide 21
BFS and Shortest Path Problem
Given any source vertex s, BFS visits the other
vertices at increasing distances away from s. In doing
so, BFS discovers paths from s to other vertices
What do we mean by “distance”? The number of
edges on a path from s
2
4
3
5
1
7
6
9
8
0
Consider s=vertex 1
Nodes at distance 1?
2, 3, 7, 91
1
1
1
2
22
2
s
Example
Nodes at distance 2?
8, 6, 5, 4
Nodes at distance 3?
0 21
Graph & BFS / Slide 22
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
Note: might also build a forest if graph is
not connected
22
Graph & BFS / Slide 23
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.
23
Graph & BFS / Slide 24
Breadth-First Search
Every vertex of a graph contains a color at
every moment:
White vertices have not been discovered
 All vertices start with white initially
Grey vertices are discovered but not fully explored
 They may be adjacent to white vertices
Black vertices are discovered and fully explored
 They are adjacent only to black and gray vertices
Explore vertices by scanning adjacency list of
grey vertices
24
Graph & BFS / Slide 25
Breadth-First Search: The Code
Data: color[V], prev[V],d[V]
BFS(G) // starts from here
{
for each vertex u ∈ V-
{s}
{
color[u]=WHITE;
prev[u]=NIL;
d[u]=inf;
}
color[s]=GRAY;
d[s]=0; prev[s]=NIL;
Q=empty;
ENQUEUE(Q,s);
While(Q not empty)
{
u = DEQUEUE(Q);
for each v ∈ adj[u]{
if (color[v] ==
WHITE){
color[v] = GREY;
d[v] = d[u] + 1;
prev[v] = u;
Enqueue(Q, v);
}
}
color[u] = BLACK;
}
}
25
Graph & BFS / Slide 26
Breadth-First Search: Example
∞
∞
∞
∞
∞
∞
∞
∞
r s t u
v w x y
Vertex r s t u v w x y
color W W W W W W W W
d ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
prev nil nil nil nil nil nil nil nil
26
Graph & BFS / Slide 27
Breadth-First Search: Example
∞
∞
0
∞
∞
∞
∞
∞
r s t u
v w x y
sQ:
vertex r s t u v w x y
Color W G W W W W W W
d ∞ 0 ∞ ∞ ∞ ∞ ∞ ∞
prev nil nil nil nil nil nil nil nil
27
Graph & BFS / Slide 28
Breadth-First Search: Example
1
∞
0
1
∞
∞
∞
∞
r s t u
v w x y
w rsQ:
vertex r s t u v w x y
Color G B W W W G W W
d 1 0 ∞ ∞ ∞ 1 ∞ ∞
prev s nil nil nil nil s nil nil
28
Graph & BFS / Slide 29
Breadth-First Search: Example
1
∞
0
1
2
2
∞
∞
r s t u
v w x y
t xw rsQ:
vertex r s t u V w X y
Color G B G W W B G W
d 1 0 2 ∞ ∞ 1 2 ∞
prev s nil w nil nil s w nil
29
Graph & BFS / Slide 30
Breadth-First Search: Example
1
2
0
1
2
2
∞
∞
r s t u
v w x y
vt xw rsQ:
vertex r s t u v w x y
Color B B G W G B G W
d 1 0 2 ∞ 2 1 2 ∞
prev s nil w nil r s w nil
30
Graph & BFS / Slide 31
Breadth-First Search: Example
1
2
0
1
2
2
3
∞
r s t u
v w x y
uvt xw rsQ:
vertex r s t u v w x y
Color B B B G G B G W
d 1 0 2 3 2 1 2 ∞
prev s nil w t r s w nil
31
Graph & BFS / Slide 32
Breadth-First Search: Example
1
2
0
1
2
2
3
3
r s t u
v w x y
yuvt xw rsQ:
vertex r s t u v w x y
Color B B B G G B B G
d 1 0 2 3 2 1 2 3
prev s nil w t r s w x
32
Graph & BFS / Slide 33
Breadth-First Search: Example
1
2
0
1
2
2
3
3
r s t u
v w x y
yuvt xw rsQ:
vertex r s t u v w x y
Color B B B G B B B G
d 1 0 2 3 2 1 2 3
prev s nil w t r s w x
33
Graph & BFS / Slide 34
Breadth-First Search: Example
1
2
0
1
2
2
3
3
r s t u
v w x y
yuvt xw rsQ:
vertex r s t u v w x y
Color B B B B B B B G
d 1 0 2 3 2 1 2 3
prev s nil w t r s w x
34
Graph & BFS / Slide 35
Breadth-First Search: Example
1
2
0
1
2
2
3
3
r s t u
v w x y
yuvt xw rsQ:
vertex r s t u v w x y
Color B B B G B B B B
d 1 0 2 3 2 1 2 3
prev s nil w t r s w x
35
Graph & BFS / Slide 36
BFS: The Code (again)
Data: color[V], prev[V],d[V]
BFS(G) // starts from here
{
for each vertex u ∈ V-
{s}
{
color[u]=WHITE;
prev[u]=NIL;
d[u]=inf;
}
color[s]=GRAY;
d[s]=0; prev[s]=NIL;
Q=empty;
ENQUEUE(Q,s);
While(Q not empty)
{
u = DEQUEUE(Q);
for each v ∈ adj[u]{
if (color[v] ==
WHITE){
color[v] = GREY;
d[v] = d[u] + 1;
prev[v] = u;
Enqueue(Q, v);
}
}
color[u] = BLACK;
}
}
36
Graph & BFS / Slide 37
Breadth-First Search: Print Path
Data: color[V], prev[V],d[V]
Print-Path(G, s, v)
{
if(v==s)
print(s)
else if(prev[v]==NIL)
print(No path);
else{
Print-Path(G,s,prev[v]);
print(v);
}
}
37
Graph & BFS / Slide 38
Amortized Analysis
Stack with 3 operations:
Push, Pop, Multi-pop
What will be the complexity if “n”
operations are performed?
38
Graph & BFS / Slide 39
BFS: Complexity
Data: color[V], prev[V],d[V]
BFS(G) // starts from here
{
for each vertex u ∈ V-
{s}
{
color[u]=WHITE;
prev[u]=NIL;
d[u]=inf;
}
color[s]=GRAY;
d[s]=0; prev[s]=NIL;
Q=empty;
ENQUEUE(Q,s);
While(Q not empty)
{
u = DEQUEUE(Q);
for each v ∈ adj[u]{
if(color[v] == WHITE){
color[v] = GREY;
d[v] = d[u] + 1;
prev[v] = u;
Enqueue(Q, v);
}
}
color[u] = BLACK;
}
}
O(V)
O(V)
u = every vertex, but only once
(Why?)
What will be the running time?
Total running time: O(V+E)39
Graph & BFS / Slide 40
Breadth-First Search: Properties
BFS calculates the shortest-path distance to
the source node
Shortest-path distance δ(s,v) = minimum number
of edges from s to v, or ∞ if v not reachable from s
Proof given in the book (p. 472-5)
BFS builds breadth-first tree, in which paths to
root represent shortest paths in G
Thus can use BFS to calculate shortest path from
one vertex to another in O(V+E) time
40
Graph & BFS / Slide 41
Application of BFS
Find the shortest path in an
undirected/directed unweighted graph.
Find the bipartiteness of a graph.
Find cycle in a graph.
Find the connectedness of a graph.
41

More Related Content

What's hot

Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Shuvongkor Barman
 
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra Sahil Kumar
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data StructureKeno benti
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First SearchKevin Jadiya
 
Topological Sorting
Topological SortingTopological Sorting
Topological SortingShahDhruv21
 
Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]DEEPIKA T
 
Breadth First Search (BFS)
Breadth First Search (BFS)Breadth First Search (BFS)
Breadth First Search (BFS)Dhrumil Panchal
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structuresSavit Chandra
 
Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithmsSaga Valsalan
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)Elavarasi K
 
A presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithmA presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithmGaurav Kolekar
 
Breadth first search (Bfs)
Breadth first search (Bfs)Breadth first search (Bfs)
Breadth first search (Bfs)Ishucs
 
Prim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning treePrim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning treeoneous
 
Depth firstsearchalgorithm
Depth firstsearchalgorithmDepth firstsearchalgorithm
Depth firstsearchalgorithmhansa khan
 

What's hot (20)

Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
 
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
ADA - Minimum Spanning Tree Prim Kruskal and Dijkstra
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
 
Topological Sorting
Topological SortingTopological Sorting
Topological Sorting
 
Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]
 
Breadth First Search (BFS)
Breadth First Search (BFS)Breadth First Search (BFS)
Breadth First Search (BFS)
 
Dfs
DfsDfs
Dfs
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
 
Heaps & priority queues
Heaps & priority queuesHeaps & priority queues
Heaps & priority queues
 
Bfs new
Bfs newBfs new
Bfs new
 
Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithms
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
 
A presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithmA presentation on prim's and kruskal's algorithm
A presentation on prim's and kruskal's algorithm
 
Hash table
Hash tableHash table
Hash table
 
Breadth first search (Bfs)
Breadth first search (Bfs)Breadth first search (Bfs)
Breadth first search (Bfs)
 
Prim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning treePrim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning tree
 
Depth firstsearchalgorithm
Depth firstsearchalgorithmDepth firstsearchalgorithm
Depth firstsearchalgorithm
 

Similar to Breadth first search

Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashingVictor Palmar
 
lec 09-graphs-bfs-dfs.ppt
lec 09-graphs-bfs-dfs.pptlec 09-graphs-bfs-dfs.ppt
lec 09-graphs-bfs-dfs.pptTalhaFarooqui12
 
graph.pptx
graph.pptxgraph.pptx
graph.pptxhijigaf
 
Talk on Graph Theory - I
Talk on Graph Theory - ITalk on Graph Theory - I
Talk on Graph Theory - IAnirudh Raja
 
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfClass01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfChristianKapsales1
 
Graph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxGraph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxbashirabdullah789
 
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringGraph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringSaurabh Kaushik
 
Graph ASS DBATU.pptx
Graph ASS DBATU.pptxGraph ASS DBATU.pptx
Graph ASS DBATU.pptxARVIND SARDAR
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx pptDhruvilSTATUS
 
BFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal AlgorithmBFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal AlgorithmMSA Technosoft
 
lecture 17
lecture 17lecture 17
lecture 17sajinsc
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structurePooja Bhojwani
 

Similar to Breadth first search (20)

Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashing
 
Graphs
GraphsGraphs
Graphs
 
lec 09-graphs-bfs-dfs.ppt
lec 09-graphs-bfs-dfs.pptlec 09-graphs-bfs-dfs.ppt
lec 09-graphs-bfs-dfs.ppt
 
graph.pptx
graph.pptxgraph.pptx
graph.pptx
 
Graph
GraphGraph
Graph
 
Talk on Graph Theory - I
Talk on Graph Theory - ITalk on Graph Theory - I
Talk on Graph Theory - I
 
Graphs
GraphsGraphs
Graphs
 
09-graphs.ppt
09-graphs.ppt09-graphs.ppt
09-graphs.ppt
 
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfClass01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
 
Graph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptxGraph Representation, DFS and BFS Presentation.pptx
Graph Representation, DFS and BFS Presentation.pptx
 
graph ASS (1).ppt
graph ASS (1).pptgraph ASS (1).ppt
graph ASS (1).ppt
 
Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]
 
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringGraph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
 
Graph ASS DBATU.pptx
Graph ASS DBATU.pptxGraph ASS DBATU.pptx
Graph ASS DBATU.pptx
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx ppt
 
BFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal AlgorithmBFS, Breadth first search | Search Traversal Algorithm
BFS, Breadth first search | Search Traversal Algorithm
 
lecture 17
lecture 17lecture 17
lecture 17
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
Chapter 23 aoa
Chapter 23 aoaChapter 23 aoa
Chapter 23 aoa
 
Graphs
GraphsGraphs
Graphs
 

Recently uploaded

IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacingjaychoudhary37
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 

Recently uploaded (20)

IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacing
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 

Breadth first search

  • 2. Graph & BFS / Slide 2 Graphs Extremely useful tool in modeling problems Consist of: Vertices Edges D E A C F B Vertex Edge Vertices can be considered “sites” or locations. Edges represent connections. 2
  • 3. Graph & BFS / Slide 3 Application Air flight system • Each vertex represents a city • Each edge represents a direct flight between two cities • A query on direct flights = a query on whether an edge exists • A query on how to get to a location = does a path exist from A to B • We can even associate costs to edges (weighted graphs), then ask “what is the cheapest path from A to B” 3
  • 4. Graph & BFS / Slide 4 Definition A graph G=(V, E) consists a set of vertices, V, and a set of edges, E. Each edge is a pair of (v, w), where v, w belongs to V If the pair is unordered, the graph is undirected; otherwise it is directed {c,f} {a,c}{a,b} {b,d} {c,d} {e,f} {b,e} An undirected graph 4
  • 5. Graph & BFS / Slide 5 Definition Complete Graph How many edges are there in an N-vertex complete graph? Bipartite Graph What is its property? How can we detect it? Path Tour Degree of a vertices Indegree Outdegree Indegree+outdegree = Even (why??) 5
  • 6. Graph & BFS / Slide 6 Graph Variations Variations: A connected graph has a path from every vertex to every other In an undirected graph:  Edge (u,v) = Edge (v,u)  No self-loops In a directed graph:  Edge (u,v) goes from vertex u to vertex v, notated u→v 6
  • 7. Graph & BFS / Slide 7 Graph Variations More variations: A weighted graph associates weights with either the edges or the vertices  E.g., a road map: edges might be weighted w distance A multi-graph allows multiple edges between the same vertices  E.g., the call graph in a program (a function can get called from multiple points in another function) 7
  • 8. Graph & BFS / Slide 8 Graphs We will typically express running times in terms of |E| and |V| (often dropping the |’s) If |E| ≈ |V|2 the graph is dense If |E| ≈ |V| the graph is sparse If you know you are dealing with dense or sparse graphs, different data structures may make sense 8
  • 9. Graph & BFS / Slide 9 Graph Representation Two popular computer representations of a graph. Both represent the vertex set and the edge set, but in different ways. 1. Adjacency Matrix Use a 2D matrix to represent the graph 1. Adjacency List Use a 1D array of linked lists 9
  • 10. Graph & BFS / Slide 10 Adjacency Matrix 2D array A[0..n-1, 0..n-1], where n is the number of vertices in the graph Each row and column is indexed by the vertex id e,g a=0, b=1, c=2, d=3, e=4 A[i][j]=1 if there is an edge connecting vertices i and j; otherwise, A[i][j]=0 The storage requirement is Θ(n2 ). It is not efficient if the graph has few edges. An adjacency matrix is an appropriate representation if the graph is dense: |E|=Θ(|V|2 ) We can detect in O(1) time whether two vertices are connected. 10
  • 11. Graph & BFS / Slide 11 Simple Questions on Adjacency Matrix Is there a direct link between A and B? What is the indegree and outdegree for a vertex A? How many nodes are directly connected to vertex A? Is it an undirected graph or directed graph? Suppose ADJ is an NxN matrix. What will be the result if we create another matrix ADJ2 where ADJ2=ADJxADJ? 11
  • 12. Graph & BFS / Slide 12 Adjacency List If the graph is not dense, in other words, sparse, a better solution is an adjacency list The adjacency list is an array A[0..n-1] of lists, where n is the number of vertices in the graph. Each array entry is indexed by the vertex id Each list A[i] stores the ids of the vertices adjacent to vertex i 12
  • 13. Graph & BFS / Slide 13 Adjacency Matrix Example 2 4 3 5 1 7 6 9 8 0 0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 1 0 0 0 1 0 1 2 0 1 0 0 1 0 0 0 1 0 3 0 1 0 0 1 1 0 0 0 0 4 0 0 1 1 0 0 0 0 0 0 5 0 0 0 1 0 0 1 0 0 0 6 0 0 0 0 0 1 0 1 0 0 7 0 1 0 0 0 0 1 0 0 0 8 1 0 1 0 0 0 0 0 0 1 9 0 1 0 0 0 0 0 0 1 0 13
  • 14. Graph & BFS / Slide 14 Adjacency List Example 2 4 3 5 1 7 6 9 8 0 0 1 2 3 4 5 6 7 8 9 2 3 7 9 8 1 4 8 1 4 5 2 3 3 6 5 7 1 6 0 2 9 1 8 14
  • 15. Graph & BFS / Slide 15 The array takes up Θ(n) space Define degree of v, deg(v), to be the number of edges incident to v. Then, the total space to store the graph is proportional to: An edge e={u,v} of the graph contributes a count of 1 to deg(u) and contributes a count 1 to deg(v) Therefore, Σvertex vdeg(v) = 2m, where m is the total number of edges In all, the adjacency list takes up Θ(n+m) space If m = O(n2 ) (i.e. dense graphs), both adjacent matrix and adjacent lists use Θ(n2 ) space. If m = O(n), adjacent list outperform adjacent matrix However, one cannot tell in O(1) time whether two vertices are connected Storage of Adjacency List ∑v v vertex )deg( 15
  • 16. Graph & BFS / Slide 16 Adjacency List vs. Matrix Adjacency List More compact than adjacency matrices if graph has few edges Requires more time to find if an edge exists Adjacency Matrix Always require n2 space  This can waste a lot of space if the number of edges are sparse Can quickly find if an edge exists 16
  • 17. Graph & BFS / Slide 17 Path between Vertices A path is a sequence of vertices (v0, v1, v2,… vk) such that: For 0 ≤ i < k, {vi, vi+1} is an edge Note: a path is allowed to go through the same vertex or the same edge any number of times! The length of a path is the number of edges on the path 17
  • 18. Graph & BFS / Slide 18 Types of paths A path is simple if and only if it does not contain a vertex more than once. A path is a cycle if and only if v0= vk  The beginning and end are the same vertex! A path contains a cycle as its sub-path if some vertex appears twice or more 18
  • 19. Graph & BFS / Slide 19 Path Examples 1. {a,c,f,e} 2. {a,b,d,c,f,e} 3. {a, c, d, b, d, c, f, e} 4. {a,c,d,b,a} 5. {a,c,f,e,b,d,c,a} Are these paths? Any cycles? What is the path’s length? 19
  • 20. Graph & BFS / Slide 20 Graph Traversal Application example Given a graph representation and a vertex s in the graph Find paths from s to other vertices Two common graph traversal algorithms  Breadth-First Search (BFS) Find the shortest paths in an unweighted graph  Depth-First Search (DFS) Topological sort Find strongly connected components 20
  • 21. Graph & BFS / Slide 21 BFS and Shortest Path Problem Given any source vertex s, BFS visits the other vertices at increasing distances away from s. In doing so, BFS discovers paths from s to other vertices What do we mean by “distance”? The number of edges on a path from s 2 4 3 5 1 7 6 9 8 0 Consider s=vertex 1 Nodes at distance 1? 2, 3, 7, 91 1 1 1 2 22 2 s Example Nodes at distance 2? 8, 6, 5, 4 Nodes at distance 3? 0 21
  • 22. Graph & BFS / Slide 22 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 Note: might also build a forest if graph is not connected 22
  • 23. Graph & BFS / Slide 23 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. 23
  • 24. Graph & BFS / Slide 24 Breadth-First Search Every vertex of a graph contains a color at every moment: White vertices have not been discovered  All vertices start with white initially Grey vertices are discovered but not fully explored  They may be adjacent to white vertices Black vertices are discovered and fully explored  They are adjacent only to black and gray vertices Explore vertices by scanning adjacency list of grey vertices 24
  • 25. Graph & BFS / Slide 25 Breadth-First Search: The Code Data: color[V], prev[V],d[V] BFS(G) // starts from here { for each vertex u ∈ V- {s} { color[u]=WHITE; prev[u]=NIL; d[u]=inf; } color[s]=GRAY; d[s]=0; prev[s]=NIL; Q=empty; ENQUEUE(Q,s); While(Q not empty) { u = DEQUEUE(Q); for each v ∈ adj[u]{ if (color[v] == WHITE){ color[v] = GREY; d[v] = d[u] + 1; prev[v] = u; Enqueue(Q, v); } } color[u] = BLACK; } } 25
  • 26. Graph & BFS / Slide 26 Breadth-First Search: Example ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ r s t u v w x y Vertex r s t u v w x y color W W W W W W W W d ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ prev nil nil nil nil nil nil nil nil 26
  • 27. Graph & BFS / Slide 27 Breadth-First Search: Example ∞ ∞ 0 ∞ ∞ ∞ ∞ ∞ r s t u v w x y sQ: vertex r s t u v w x y Color W G W W W W W W d ∞ 0 ∞ ∞ ∞ ∞ ∞ ∞ prev nil nil nil nil nil nil nil nil 27
  • 28. Graph & BFS / Slide 28 Breadth-First Search: Example 1 ∞ 0 1 ∞ ∞ ∞ ∞ r s t u v w x y w rsQ: vertex r s t u v w x y Color G B W W W G W W d 1 0 ∞ ∞ ∞ 1 ∞ ∞ prev s nil nil nil nil s nil nil 28
  • 29. Graph & BFS / Slide 29 Breadth-First Search: Example 1 ∞ 0 1 2 2 ∞ ∞ r s t u v w x y t xw rsQ: vertex r s t u V w X y Color G B G W W B G W d 1 0 2 ∞ ∞ 1 2 ∞ prev s nil w nil nil s w nil 29
  • 30. Graph & BFS / Slide 30 Breadth-First Search: Example 1 2 0 1 2 2 ∞ ∞ r s t u v w x y vt xw rsQ: vertex r s t u v w x y Color B B G W G B G W d 1 0 2 ∞ 2 1 2 ∞ prev s nil w nil r s w nil 30
  • 31. Graph & BFS / Slide 31 Breadth-First Search: Example 1 2 0 1 2 2 3 ∞ r s t u v w x y uvt xw rsQ: vertex r s t u v w x y Color B B B G G B G W d 1 0 2 3 2 1 2 ∞ prev s nil w t r s w nil 31
  • 32. Graph & BFS / Slide 32 Breadth-First Search: Example 1 2 0 1 2 2 3 3 r s t u v w x y yuvt xw rsQ: vertex r s t u v w x y Color B B B G G B B G d 1 0 2 3 2 1 2 3 prev s nil w t r s w x 32
  • 33. Graph & BFS / Slide 33 Breadth-First Search: Example 1 2 0 1 2 2 3 3 r s t u v w x y yuvt xw rsQ: vertex r s t u v w x y Color B B B G B B B G d 1 0 2 3 2 1 2 3 prev s nil w t r s w x 33
  • 34. Graph & BFS / Slide 34 Breadth-First Search: Example 1 2 0 1 2 2 3 3 r s t u v w x y yuvt xw rsQ: vertex r s t u v w x y Color B B B B B B B G d 1 0 2 3 2 1 2 3 prev s nil w t r s w x 34
  • 35. Graph & BFS / Slide 35 Breadth-First Search: Example 1 2 0 1 2 2 3 3 r s t u v w x y yuvt xw rsQ: vertex r s t u v w x y Color B B B G B B B B d 1 0 2 3 2 1 2 3 prev s nil w t r s w x 35
  • 36. Graph & BFS / Slide 36 BFS: The Code (again) Data: color[V], prev[V],d[V] BFS(G) // starts from here { for each vertex u ∈ V- {s} { color[u]=WHITE; prev[u]=NIL; d[u]=inf; } color[s]=GRAY; d[s]=0; prev[s]=NIL; Q=empty; ENQUEUE(Q,s); While(Q not empty) { u = DEQUEUE(Q); for each v ∈ adj[u]{ if (color[v] == WHITE){ color[v] = GREY; d[v] = d[u] + 1; prev[v] = u; Enqueue(Q, v); } } color[u] = BLACK; } } 36
  • 37. Graph & BFS / Slide 37 Breadth-First Search: Print Path Data: color[V], prev[V],d[V] Print-Path(G, s, v) { if(v==s) print(s) else if(prev[v]==NIL) print(No path); else{ Print-Path(G,s,prev[v]); print(v); } } 37
  • 38. Graph & BFS / Slide 38 Amortized Analysis Stack with 3 operations: Push, Pop, Multi-pop What will be the complexity if “n” operations are performed? 38
  • 39. Graph & BFS / Slide 39 BFS: Complexity Data: color[V], prev[V],d[V] BFS(G) // starts from here { for each vertex u ∈ V- {s} { color[u]=WHITE; prev[u]=NIL; d[u]=inf; } color[s]=GRAY; d[s]=0; prev[s]=NIL; Q=empty; ENQUEUE(Q,s); While(Q not empty) { u = DEQUEUE(Q); for each v ∈ adj[u]{ if(color[v] == WHITE){ color[v] = GREY; d[v] = d[u] + 1; prev[v] = u; Enqueue(Q, v); } } color[u] = BLACK; } } O(V) O(V) u = every vertex, but only once (Why?) What will be the running time? Total running time: O(V+E)39
  • 40. Graph & BFS / Slide 40 Breadth-First Search: Properties BFS calculates the shortest-path distance to the source node Shortest-path distance δ(s,v) = minimum number of edges from s to v, or ∞ if v not reachable from s Proof given in the book (p. 472-5) BFS builds breadth-first tree, in which paths to root represent shortest paths in G Thus can use BFS to calculate shortest path from one vertex to another in O(V+E) time 40
  • 41. Graph & BFS / Slide 41 Application of BFS Find the shortest path in an undirected/directed unweighted graph. Find the bipartiteness of a graph. Find cycle in a graph. Find the connectedness of a graph. 41