0% found this document useful (0 votes)
27 views

Graph Presentation

This document provides an overview of graph terminology and data structures before explaining breadth-first search (BFS) and depth-first search (DFS) algorithms. It defines key graph concepts like vertices, edges, and adjacency representations. BFS explores all neighbors of a starting node before moving to the next level, producing the shortest path tree, while DFS explores as far as possible along each branch before backtracking. Both have the same time complexity but BFS uses more space. Examples show the process for each. Applications include routing, mapping, and topological sorting.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Graph Presentation

This document provides an overview of graph terminology and data structures before explaining breadth-first search (BFS) and depth-first search (DFS) algorithms. It defines key graph concepts like vertices, edges, and adjacency representations. BFS explores all neighbors of a starting node before moving to the next level, producing the shortest path tree, while DFS explores as far as possible along each branch before backtracking. Both have the same time complexity but BFS uses more space. Examples show the process for each. Applications include routing, mapping, and topological sorting.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Graph

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

graph Adjacency list Adjacency matrix

5
Graph representation – directed

graph Adjacency list 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.

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.

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.

❑ Most of the mapping software (GOOGLE maps, YAHOO(?)


maps) should be using these algorithms.
■ Applications of DFS
❑ Topologically sorting a directed acyclic graph.
■ List the graph elements in such an order that all the nodes are listed before
nodes to which they have outgoing edges.
❑ Finding the strongly connected components of a directed graph.
■ List all the sub graphs of a strongly connected graph which
themselves are strongly connected.

13
Thank You

14

You might also like