SlideShare una empresa de Scribd logo
1 de 19
Graphs
Breadth First Search
&
Depth First Search
Submitted By:
Jailalita Gautam
Contents







10/27/13

Overview of Graph terminology.
Graph representation.
Breadth first search.
Depth first search.
Applications of BFS and DFS.
References.

NITTTR CHD

2
Graph terminology - overview


A graph consists of











set of vertices V = {v1, v2, ….. vn}
set of edges that connect the vertices E ={e1, e2, …. em}

Two vertices in a graph are adjacent if there is an
edge connecting the vertices.
Two vertices are on a path if there is a sequences
of vertices beginning with the first one and ending
with the second one
Graphs with ordered edges are directed. For
directed graphs, vertices have in and out degrees.
Weighted Graphs have values associated with
edges.

10/27/13

NITTTR CHD

3
Graph representation






There are two standard ways to represent a graph
G=(V,E) : as collection of adjacency list or as an
adjacency matrix.
Adjacency list preferred for sparse graphs- those for
which |E| much less than |V|^2.
Adjacency matrix preferred for dense graphs- |E| is
close to |V|^2.

10/27/13

NITTTR CHD

4
Graph representation – undirected

graph

10/27/13

Adjacency list

NITTTR CHD

Adjacency matrix

5
Graph representation – directed

graph

10/27/13

Adjacency list

NITTTR CHD

Adjacency matrix

6
Breadth first search


Given










a graph G=(V,E) – set of vertices and edges
a distinguished source vertex s

Breadth first search systematically explores the
edges of G to discover every vertex that is
reachable from s.
It also produces a ‘breadth first tree’ with root s that
contains all the vertices reachable from s.
For any vertex v reachable from s, the path in the
breadth first tree corresponds to the shortest path in
graph G from s to v.
It works on both directed and undirected graphs.
However, we will explore only directed graphs.

10/27/13

NITTTR CHD

7
Breadth first search - concepts







To keep track of progress, it colors each
vertex - white, gray or black.
All vertices start white.
A vertex discovered first time during the
search becomes nonwhite.
All vertices adjacent to black ones are
discovered. Whereas, gray ones may have
some white adjacent vertices.

10/27/13

NITTTR CHD

8
BFS – How it produces a Breadth first tree



The tree initially contains only root. – s
Whenever a vertex v is discovered in
scanning adjacency list of vertex u


10/27/13

Vertex v and edge (u,v) are added to the tree.

NITTTR CHD

9
BFS - algorithm
BFS(G, s)
// G is the graph and s is the starting node
1 for each vertex u ∈ V [G] - {s}
2
do color[u] ← WHITE
// color of vertex u
3
d[u] ← ∞
// distance from source s to vertex u
4
π[u] ← NIL
// predecessor of u
5 color[s] ← GRAY
6 d[s] ← 0
7 π[s] ← NIL
8 Q←Ø
// Q is a FIFO - queue
9 ENQUEUE(Q, s)
10 while Q ≠ Ø
// iterates as long as there are gray vertices. Lines 10-18
11
do u ← DEQUEUE(Q)
12
for each v ∈ Adj [u]
13
do if color[v] = WHITE
// discover the undiscovered adjacent vertices
14
then color[v] ← GRAY
// enqueued whenever painted gray
15
d[v] ← d[u] + 1
16
π[v] ← u
17
ENQUEUE(Q, v)
18
color[u] ← BLACK
// painted black whenever dequeued

10/27/13

NITTTR CHD

10
Breadth First Search - example

10/27/13

NITTTR CHD

11
Breadth first search - analysis






Enqueue and Dequeue happen only once for
each node. - O(V).
Total time spent in scanning adjacency lists
is O(E) .
Initialization overhead O(V)
Total runtime O(V+E)

10/27/13

NITTTR CHD

12
Depth first search





It searches ‘deeper’ the graph when possible.
Starts at the selected node and explores as far as
possible along each branch before backtracking.
Vertices go through white, gray and black stages of
color.






White – initially
Gray – when discovered first
Black – when finished i.e. the adjacency list of the vertex is
completely examined.

Also records timestamps for each vertex



10/27/13

d[v]
f[v]

when the vertex is first discovered
when the vertex is finished
NITTTR CHD

13
Depth first search - algorithm
DFS(G)
1 for each vertex u ∈ V [G]
2
do color[u] ← WHITE
3
π[u] ← NIL
4 time ← 0
5 for each vertex u ∈ V [G]
6
do if color[u] = WHITE
7
then DFS-VISIT(u)

// color all vertices white, set their parents NIL
// zero out time
// call only for unexplored vertices
// this may result in multiple sources

DFS-VISIT(u)
1 color[u] ← GRAY ▹White vertex u has just been discovered.
2 time ← time +1
3 d[u] time
// record the discovery time
4 for each v ∈ Adj[u]
▹Explore edge(u, v).
5
do if color[v] = WHITE
6
then π[v] ← u
// set the parent value
7
DFS-VISIT(v)
// recursive call
8 color[u] BLACK
▹ Blacken u; it is finished.
9 f [u] ▹ time ← time +1
10/27/13

NITTTR CHD

14
Depth first search – example

10/27/13

NITTTR CHD

15
Depth first search - analysis
Lines 1-3, initialization take time Θ(V).
 Lines 5-7 take time Θ(V), excluding the time to call
the DFS-VISIT.
 DFS-VISIT is called only once for each node (since
it’s called only for white nodes and the first step in it
is to paint the node gray).
 Loop on line 4-7 is executed |Adj(v)| times. Since
∑vєV |Adj(v)| = Ө (E),
the total cost of executing lines 4-7 of DFS-VISIT is
θ(E).


The total cost of DFS is θ(V+E)
10/27/13

NITTTR CHD

16
BFS and DFS - comparison






Space complexity of DFS is lower than that of BFS.
Time complexity of both is same – O(|V|+|E|).
The behavior differs for graphs where not all the
vertices can be reached from the given vertex s.
Predecessor subgraphs produced by DFS may be
different than those produced by BFS. The BFS
product is just one tree whereas the DFS product
may be multiple trees.

10/27/13

NITTTR CHD

17
BFS and DFS – possible applications




Possible to use in routing / exploration. E.g.,
 I want to explore all the nearest pizza places and want to go to
the nearest one with only two intersections.
 Find distance from my factory to every delivery center.
 Most of the mapping software (GOOGLE maps, YAHOO(?)
maps) should be using these algorithms.
Applications of DFS
 Topologically sorting a directed acyclic graph.




Finding the strongly connected components of a directed graph.


10/27/13

List the graph elements in such an order that all the nodes are listed
before nodes to which they have outgoing edges.
List all the sub graphs of a strongly connected graph which
themselves are strongly connected.

NITTTR CHD

18
References







Data structures with C++ using STL by Ford,
William; Topp, William; Prentice Hall.
Introduction to Algorithms by Cormen,
Thomas et. al., The MIT press.
http://en.wikipedia.org/wiki/Graph_theory
http://en.wikipedia.org/wiki/Depth_first_search

10/27/13

NITTTR CHD

19

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Graph traversal-BFS & DFS
Graph traversal-BFS & DFSGraph traversal-BFS & DFS
Graph traversal-BFS & DFS
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
 
SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 
BFS
BFSBFS
BFS
 
Breadth First Search (BFS)
Breadth First Search (BFS)Breadth First Search (BFS)
Breadth First Search (BFS)
 
Red black trees presentation
Red black trees presentationRed black trees presentation
Red black trees presentation
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
 
Dijkstra’s algorithm
Dijkstra’s algorithmDijkstra’s algorithm
Dijkstra’s algorithm
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Depth firstsearchalgorithm
Depth firstsearchalgorithmDepth firstsearchalgorithm
Depth firstsearchalgorithm
 
Dijkstra’S Algorithm
Dijkstra’S AlgorithmDijkstra’S Algorithm
Dijkstra’S Algorithm
 
Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithms
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Dijkstra's algorithm presentation
Dijkstra's algorithm presentationDijkstra's algorithm presentation
Dijkstra's algorithm presentation
 
Bellman ford Algorithm
Bellman ford AlgorithmBellman ford Algorithm
Bellman ford Algorithm
 
Prim Algorithm and kruskal algorithm
Prim Algorithm and kruskal algorithmPrim Algorithm and kruskal algorithm
Prim Algorithm and kruskal algorithm
 
Dijkstra
DijkstraDijkstra
Dijkstra
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 

Destacado

Bfs and dfs in data structure
Bfs and dfs in  data structure Bfs and dfs in  data structure
Bfs and dfs in data structure Ankit Kumar Singh
 
2.5 bfs & dfs 02
2.5 bfs & dfs 022.5 bfs & dfs 02
2.5 bfs & dfs 02Krish_ver2
 
Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalAmrinder Arora
 
Heuristic Search
Heuristic SearchHeuristic Search
Heuristic Searchbutest
 
Heuristic Search Techniques {Artificial Intelligence}
Heuristic Search Techniques {Artificial Intelligence}Heuristic Search Techniques {Artificial Intelligence}
Heuristic Search Techniques {Artificial Intelligence}FellowBuddy.com
 
Ch2 3-informed (heuristic) search
Ch2 3-informed (heuristic) searchCh2 3-informed (heuristic) search
Ch2 3-informed (heuristic) searchchandsek666
 
artificial intelligence
artificial intelligenceartificial intelligence
artificial intelligencevallibhargavi
 
5.5 back tracking
5.5 back tracking5.5 back tracking
5.5 back trackingKrish_ver2
 
Data structures and algorithms lab7
Data structures and algorithms lab7Data structures and algorithms lab7
Data structures and algorithms lab7Bianca Teşilă
 
Depth First Search, Breadth First Search and Best First Search
Depth First Search, Breadth First Search and Best First SearchDepth First Search, Breadth First Search and Best First Search
Depth First Search, Breadth First Search and Best First SearchAdri Jovin
 
Mca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graphMca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graphRai University
 
Distributed Graph Algorithms
Distributed Graph AlgorithmsDistributed Graph Algorithms
Distributed Graph AlgorithmsSaurav Kumar
 

Destacado (20)

Bfs and dfs in data structure
Bfs and dfs in  data structure Bfs and dfs in  data structure
Bfs and dfs in data structure
 
Breadth first search
Breadth first searchBreadth first search
Breadth first search
 
2.5 bfs & dfs 02
2.5 bfs & dfs 022.5 bfs & dfs 02
2.5 bfs & dfs 02
 
DFS BFS and UCS in R
DFS BFS and UCS in RDFS BFS and UCS in R
DFS BFS and UCS in R
 
Graph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search TraversalGraph Traversal Algorithms - Depth First Search Traversal
Graph Traversal Algorithms - Depth First Search Traversal
 
Heuristic Search
Heuristic SearchHeuristic Search
Heuristic Search
 
chapter22.ppt
chapter22.pptchapter22.ppt
chapter22.ppt
 
Heuristic Search Techniques {Artificial Intelligence}
Heuristic Search Techniques {Artificial Intelligence}Heuristic Search Techniques {Artificial Intelligence}
Heuristic Search Techniques {Artificial Intelligence}
 
Ch2 3-informed (heuristic) search
Ch2 3-informed (heuristic) searchCh2 3-informed (heuristic) search
Ch2 3-informed (heuristic) search
 
artificial intelligence
artificial intelligenceartificial intelligence
artificial intelligence
 
5.5 back tracking
5.5 back tracking5.5 back tracking
5.5 back tracking
 
Normalization
NormalizationNormalization
Normalization
 
DFS & BFS Graph
DFS & BFS GraphDFS & BFS Graph
DFS & BFS Graph
 
2.5 graph dfs
2.5 graph dfs2.5 graph dfs
2.5 graph dfs
 
Data structures and algorithms lab7
Data structures and algorithms lab7Data structures and algorithms lab7
Data structures and algorithms lab7
 
2.5 dfs & bfs
2.5 dfs & bfs2.5 dfs & bfs
2.5 dfs & bfs
 
Algorithum Analysis
Algorithum AnalysisAlgorithum Analysis
Algorithum Analysis
 
Depth First Search, Breadth First Search and Best First Search
Depth First Search, Breadth First Search and Best First SearchDepth First Search, Breadth First Search and Best First Search
Depth First Search, Breadth First Search and Best First Search
 
Mca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graphMca iii dfs u-4 tree and graph
Mca iii dfs u-4 tree and graph
 
Distributed Graph Algorithms
Distributed Graph AlgorithmsDistributed Graph Algorithms
Distributed Graph Algorithms
 

Similar a Graphs bfs dfs

Analysis and design of algorithms part 3
Analysis and design of algorithms part 3Analysis and design of algorithms part 3
Analysis and design of algorithms part 3Deepak John
 
Breadth first search (Bfs)
Breadth first search (Bfs)Breadth first search (Bfs)
Breadth first search (Bfs)Ishucs
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in indiaEdhole.com
 
Graph Traversal Algorithm
Graph Traversal AlgorithmGraph Traversal Algorithm
Graph Traversal Algorithmjyothimonc
 
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
 
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
 
Depth First Search and Breadth First Search
Depth First Search and Breadth First SearchDepth First Search and Breadth First Search
Depth First Search and Breadth First SearchNisha Soms
 
Elementary Graph Algo.ppt
Elementary Graph Algo.pptElementary Graph Algo.ppt
Elementary Graph Algo.pptSazidHossain9
 
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxgraphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxwhittemorelucilla
 
Talk on Graph Theory - I
Talk on Graph Theory - ITalk on Graph Theory - I
Talk on Graph Theory - IAnirudh Raja
 
Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 7Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 7Traian Rebedea
 

Similar a Graphs bfs dfs (20)

Analysis and design of algorithms part 3
Analysis and design of algorithms part 3Analysis and design of algorithms part 3
Analysis and design of algorithms part 3
 
Chapter 23 aoa
Chapter 23 aoaChapter 23 aoa
Chapter 23 aoa
 
Bfs dfs
Bfs dfsBfs dfs
Bfs dfs
 
Breadth first search (Bfs)
Breadth first search (Bfs)Breadth first search (Bfs)
Breadth first search (Bfs)
 
B.tech admission in india
B.tech admission in indiaB.tech admission in india
B.tech admission in india
 
19-graph1 (1).ppt
19-graph1 (1).ppt19-graph1 (1).ppt
19-graph1 (1).ppt
 
Graph Traversal Algorithm
Graph Traversal AlgorithmGraph Traversal Algorithm
Graph Traversal Algorithm
 
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
 
Graps 2
Graps 2Graps 2
Graps 2
 
Graphs
GraphsGraphs
Graphs
 
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
 
Depth First Search and Breadth First Search
Depth First Search and Breadth First SearchDepth First Search and Breadth First Search
Depth First Search and Breadth First Search
 
Graphs
GraphsGraphs
Graphs
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
 
Elementary Graph Algo.ppt
Elementary Graph Algo.pptElementary Graph Algo.ppt
Elementary Graph Algo.ppt
 
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxgraphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
 
U1 L5 DAA.pdf
U1 L5 DAA.pdfU1 L5 DAA.pdf
U1 L5 DAA.pdf
 
Talk on Graph Theory - I
Talk on Graph Theory - ITalk on Graph Theory - I
Talk on Graph Theory - I
 
Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 7Algorithm Design and Complexity - Course 7
Algorithm Design and Complexity - Course 7
 
09-graphs.ppt
09-graphs.ppt09-graphs.ppt
09-graphs.ppt
 

Último

Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditSkynet Technologies
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 

Último (20)

Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance Audit
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 

Graphs bfs dfs

  • 1. Graphs Breadth First Search & Depth First Search Submitted By: Jailalita Gautam
  • 2. Contents       10/27/13 Overview of Graph terminology. Graph representation. Breadth first search. Depth first search. Applications of BFS and DFS. References. NITTTR CHD 2
  • 3. Graph terminology - overview  A graph consists of       set of vertices V = {v1, v2, ….. vn} set of edges that connect the vertices E ={e1, e2, …. em} Two vertices in a graph are adjacent if there is an edge connecting the vertices. Two vertices are on a path if there is a sequences of vertices beginning with the first one and ending with the second one Graphs with ordered edges are directed. For directed graphs, vertices have in and out degrees. Weighted Graphs have values associated with edges. 10/27/13 NITTTR CHD 3
  • 4. Graph representation    There are two standard ways to represent a graph G=(V,E) : as collection of adjacency list or as an adjacency matrix. Adjacency list preferred for sparse graphs- those for which |E| much less than |V|^2. Adjacency matrix preferred for dense graphs- |E| is close to |V|^2. 10/27/13 NITTTR CHD 4
  • 5. Graph representation – undirected graph 10/27/13 Adjacency list NITTTR CHD Adjacency matrix 5
  • 6. Graph representation – directed graph 10/27/13 Adjacency list NITTTR CHD Adjacency matrix 6
  • 7. Breadth first search  Given       a graph G=(V,E) – set of vertices and edges a distinguished source vertex s Breadth first search systematically explores the edges of G to discover every vertex that is reachable from s. It also produces a ‘breadth first tree’ with root s that contains all the vertices reachable from s. For any vertex v reachable from s, the path in the breadth first tree corresponds to the shortest path in graph G from s to v. It works on both directed and undirected graphs. However, we will explore only directed graphs. 10/27/13 NITTTR CHD 7
  • 8. Breadth first search - concepts     To keep track of progress, it colors each vertex - white, gray or black. All vertices start white. A vertex discovered first time during the search becomes nonwhite. All vertices adjacent to black ones are discovered. Whereas, gray ones may have some white adjacent vertices. 10/27/13 NITTTR CHD 8
  • 9. BFS – How it produces a Breadth first tree   The tree initially contains only root. – s Whenever a vertex v is discovered in scanning adjacency list of vertex u  10/27/13 Vertex v and edge (u,v) are added to the tree. NITTTR CHD 9
  • 10. BFS - algorithm BFS(G, s) // G is the graph and s is the starting node 1 for each vertex u ∈ V [G] - {s} 2 do color[u] ← WHITE // color of vertex u 3 d[u] ← ∞ // distance from source s to vertex u 4 π[u] ← NIL // predecessor of u 5 color[s] ← GRAY 6 d[s] ← 0 7 π[s] ← NIL 8 Q←Ø // Q is a FIFO - queue 9 ENQUEUE(Q, s) 10 while Q ≠ Ø // iterates as long as there are gray vertices. Lines 10-18 11 do u ← DEQUEUE(Q) 12 for each v ∈ Adj [u] 13 do if color[v] = WHITE // discover the undiscovered adjacent vertices 14 then color[v] ← GRAY // enqueued whenever painted gray 15 d[v] ← d[u] + 1 16 π[v] ← u 17 ENQUEUE(Q, v) 18 color[u] ← BLACK // painted black whenever dequeued 10/27/13 NITTTR CHD 10
  • 11. Breadth First Search - example 10/27/13 NITTTR CHD 11
  • 12. Breadth first search - analysis    Enqueue and Dequeue happen only once for each node. - O(V). Total time spent in scanning adjacency lists is O(E) . Initialization overhead O(V) Total runtime O(V+E) 10/27/13 NITTTR CHD 12
  • 13. Depth first search    It searches ‘deeper’ the graph when possible. Starts at the selected node and explores as far as possible along each branch before backtracking. Vertices go through white, gray and black stages of color.     White – initially Gray – when discovered first Black – when finished i.e. the adjacency list of the vertex is completely examined. Also records timestamps for each vertex   10/27/13 d[v] f[v] when the vertex is first discovered when the vertex is finished NITTTR CHD 13
  • 14. Depth first search - algorithm DFS(G) 1 for each vertex u ∈ V [G] 2 do color[u] ← WHITE 3 π[u] ← NIL 4 time ← 0 5 for each vertex u ∈ V [G] 6 do if color[u] = WHITE 7 then DFS-VISIT(u) // color all vertices white, set their parents NIL // zero out time // call only for unexplored vertices // this may result in multiple sources DFS-VISIT(u) 1 color[u] ← GRAY ▹White vertex u has just been discovered. 2 time ← time +1 3 d[u] time // record the discovery time 4 for each v ∈ Adj[u] ▹Explore edge(u, v). 5 do if color[v] = WHITE 6 then π[v] ← u // set the parent value 7 DFS-VISIT(v) // recursive call 8 color[u] BLACK ▹ Blacken u; it is finished. 9 f [u] ▹ time ← time +1 10/27/13 NITTTR CHD 14
  • 15. Depth first search – example 10/27/13 NITTTR CHD 15
  • 16. Depth first search - analysis Lines 1-3, initialization take time Θ(V).  Lines 5-7 take time Θ(V), excluding the time to call the DFS-VISIT.  DFS-VISIT is called only once for each node (since it’s called only for white nodes and the first step in it is to paint the node gray).  Loop on line 4-7 is executed |Adj(v)| times. Since ∑vєV |Adj(v)| = Ө (E), the total cost of executing lines 4-7 of DFS-VISIT is θ(E).  The total cost of DFS is θ(V+E) 10/27/13 NITTTR CHD 16
  • 17. BFS and DFS - comparison     Space complexity of DFS is lower than that of BFS. Time complexity of both is same – O(|V|+|E|). The behavior differs for graphs where not all the vertices can be reached from the given vertex s. Predecessor subgraphs produced by DFS may be different than those produced by BFS. The BFS product is just one tree whereas the DFS product may be multiple trees. 10/27/13 NITTTR CHD 17
  • 18. BFS and DFS – possible applications   Possible to use in routing / exploration. E.g.,  I want to explore all the nearest pizza places and want to go to the nearest one with only two intersections.  Find distance from my factory to every delivery center.  Most of the mapping software (GOOGLE maps, YAHOO(?) maps) should be using these algorithms. Applications of DFS  Topologically sorting a directed acyclic graph.   Finding the strongly connected components of a directed graph.  10/27/13 List the graph elements in such an order that all the nodes are listed before nodes to which they have outgoing edges. List all the sub graphs of a strongly connected graph which themselves are strongly connected. NITTTR CHD 18
  • 19. References     Data structures with C++ using STL by Ford, William; Topp, William; Prentice Hall. Introduction to Algorithms by Cormen, Thomas et. al., The MIT press. http://en.wikipedia.org/wiki/Graph_theory http://en.wikipedia.org/wiki/Depth_first_search 10/27/13 NITTTR CHD 19