Graph Presentation
Graph Presentation
sBreadth First
Search &
Depth First Search
___
Ishtiak Ahmed
Moyen
2131580642
Contents
■ Overview of Graph terminology.
■ Graph representation.
■ Breadth first search.
■ Depth first search.
■ Applications of BFS
■ and DFS..
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.
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.
4
Graph representation – undirected
5
Graph representation – directed
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.
7
Breadth first search -
concepts
■ To keep track of progress, it colors each
vertex - white, gray or black.
■ All vertices start white.
8
Breadth First Search -
example
9
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
❑ d[v when the vertex is first discovered
❑ ] when the vertex is finished
f[v]
10
Depth first search –
example
11
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.
12
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.
13
Thank You
14