0% found this document useful (0 votes)
11 views40 pages

L7_Graph Traversal.pptx

The document discusses graph traversal techniques, specifically focusing on Breadth-First Search (BFS) and Depth-First Search (DFS) in the context of identifying individuals who may have been exposed to COVID-19 through contact tracing. It outlines the process of finding connected nodes in a graph, using BFS to quarantine potentially affected individuals, and modifying BFS to determine the shortest path in an unweighted graph. Additionally, it provides pseudocode for both BFS and BFS with distance calculations, emphasizing the importance of these algorithms in understanding relationships and exposure in a social network.

Uploaded by

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

L7_Graph Traversal.pptx

The document discusses graph traversal techniques, specifically focusing on Breadth-First Search (BFS) and Depth-First Search (DFS) in the context of identifying individuals who may have been exposed to COVID-19 through contact tracing. It outlines the process of finding connected nodes in a graph, using BFS to quarantine potentially affected individuals, and modifying BFS to determine the shortest path in an unweighted graph. Additionally, it provides pseudocode for both BFS and BFS with distance calculations, emphasizing the importance of these algorithms in understanding relationships and exposure in a social network.

Uploaded by

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

CSE 2106: Data Structures and Algorithms Laboratory

Graph Traversal
Dipannita Biswas
Md Mehrab Hossain Opi
Introduction 2
• Let’s consider a different graph today.
• Again each node represents a person today.
• And the edges represent if they came in contact in recent times.
• Let’s say edges represent if two people met each other in the last 7 days.

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


Introduction 3

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


Introduction 4
• Then one day someone got COVID-19.

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


Introduction 5
• To limit the virus spread, your task is to quarantine all individuals who might
have got the virus from the affected person.
• So how do we find all the people who might be affected?
• The virus can be transmitted from person to person
• All the person that has a direct or indirect connection with the patient is in
danger.

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


Introduction 6
• So we have to find these other four people.

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


Introduction 7

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


Finding Connected Nodes 8
• Let’s try to answer the first question.
• How do we find all the nodes?
• We can start by finding the adjacent nodes first.
• Then we find the adjacent of the adjacent nodes.
• We repeat until we find all the nodes.

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


Finding Connected Nodes 9
AtThen
first, find
find their
adjacent nodes.
adjacents.

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


Graph Traversal 10
• We will solve this problem using graph traversal.
• Just like an array we can traverse a graph.
• We start from any node and mark all the nodes that we can visit following
some edge.
• We will see two different traversal methods
• Breadth-First Search (BFS)
• Depth-First Search (DFS)

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


Breadth-First Search 11
• Let’s start with BFS.
• As the name suggests, we will visit all the adjacent nodes of a given node
before going to another node.
• We won’t visit the same node twice.

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


Breadth-First Search 12
• Consider the graph.
• To mark a node as visited 1
we will keep an array.
2 3 4
0 1 2 3 4 5 6 7 8 9
0 0 0 0 0 0 0 0 0 0
5 6 7

8 9

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


Breadth-First Search 13
• Let’s start with node 1. 1
• We call 1 the source node.
• And make it visited. 2 3 4

• Let’s take a variable ‘cur’.


• ‘cur’ holds the node
5 6 7
• That we have visited but whose all adjacents
are still not visited.

0 • So,
1 initially
2 3 ‘cur’4 5= 1.6
8 9
7 8 9
0 1 0 0 0 0 0 0 0 0

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


Breadth-First Search 14
• Then we visit all the adjacent nodes of cur.
• Which are 2, 3, and 4. 1

• We visited all the adjacents of 1.


• So we need to update cur. 2 3 4

• What should be the value of cur now?


• It should be one of the adjacents of 1. 5 6 7
• 2, 3, or 4.

0 1 2 3 4 5 6 7 8 9
8 9
0 1 1 1 1 0 0 0 0 0

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


Breadth-First Search 15
• As we need to traverse adjacents of all these nodes, let’s use a data
structure to store nodes that can become ‘cur’.

2 3 4
• We will take the first element from the data structure
• Remove it
• Visit all of its adjacent
• Insert them at the end of our data structure.
• So we need a data structure that can remove an element from the
beginning and insert an element at the end with good time complexity.
• Which data structure should we use then?
• The answer is queue.

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


Breadth-First Search 16
• Let’s start the BFS from the beginning again.
• To perform BFS we need
• A queue
• A temporary variable to hold a node
• A visited array.

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


BFS Simulation 17
Store Check
the front
Weallelement
We the asofvisited
startadjacent
mark 1with theof
node queue
cur. in cur
1.
And Now we
If aremove
adjacent enter
it from a loop
the queue.
cur and push itisinto
unvisited,
queue.
mark it and push it in queue. 1
2
1

2 3 4
Queue

1
2 3 4 5 6

5 6 7
Visited Array

0 1 2 3 4 5 6 7 8 9
8 9
0 0
1 1
0 1
0 0
1 0
1 1
0 0 0 0

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


BFS Simulation 18
Store the front element of the queue in cur
cur And remove
Check all the itadjacent
from the of
queue.
cur.
If a adjacent is unvisited, 1
4
3
2 mark it and push it in queue.

2 3 4
Queue

3 4 5 6 9 7

5 6 7
Visited Array

0 1 2 3 4 5 6 7 8 9
8 9
0 1 1 1 1 1 1 1
0 0 1
0

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


BFS Simulation 19
Store thequeue
As the front element
is emptyofthe
theloop
queue in cur
ends.
cur And remove
Check all the itadjacent
from the of
queue.
cur.
If a adjacent is unvisited, 1
7
8
5
9
6
4
3
2 mark it and push it in queue.

2 3 4
Queue

5 6 9 7 8

5 6 7
Visited Array

0 1 2 3 4 5 6 7 8 9
8 9
0 1 1 1 1 1 1 1 1
0 1

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


BFS Pseudocode 20
function BFS(graph, start_vertex):
create a queue Q and enqueue the start_vertex
mark start_vertex as visited

while Q is not empty:


current_vertex = dequeue from Q
process current_vertex

for each neighbor in neighbors of current_vertex:


if neighbor is not visited:
mark neighbor as visited
enqueue neighbor into Q

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


Question 21
• Let’s visit our question again.
• How many people should we quarantine?
• Run a BFS from the affected node.
• All the nodes that are marked visited should be quarantined.

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


Question 2 22

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


Path 23
• As mentioned earlier, A path is a sequence of nodes where each
adjacent pair of vertices is connected by an edge.

2 3 4

5 6 7

8 9

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


Shortest Path 24
• In an unweighted graph, the shortest path is the one with the minimum
number of edges.
• This measure represents the fewest steps or hops needed to reach from
one vertex to another.

2 3 4

5 6 7

8 9

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


Shortest Path Using BFS 25
• Can we modify our BFS algorithm to find minimum distance between two
nodes?
• We will need an extra array ‘dist’ to store the distance now.
• When we find an unvisited node, we will update its distance with
dist[cur]+1.

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


Shortest Path 26
• Again we start with 1.
1
• We will mark 1 as visited.
• And distance from source to 1 is 0. 2 3 4
• So dist[1] = 0.

5 6 7

8 9

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


BFS with Distance Simulation 27
StoreWethemark
Checkfront
all1element
We the adjacent
as visited,
start with ofnode
the queue
of
update cur.
1. in cur
distance
And Nowpush
If aremove
adjacent
and we enter
it from aqueue.
the loop
queue.
isitunvisited,
into
cur 1
mark it, update distance and push it in queue.
2
1
2 3 4
Queue

1
2 3 4 5 6

5 6 7

0 1 2 3 4 5 6 7 8 9
Visited 0 0
1 1
0 1
0 0
1 0
1 1
0 0 0 0 8 9
Distance - -0 1- 1- 1- 2- 2- - - -

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


BFS with Shortest Path Pseudocode 28
function BFS_with_Distance(graph, start_vertex):

create a queue Q and enqueue the start_vertex

create a array distances and set distances[start_vertex] = 0

mark start_vertex as visited

while Q is not empty:

current_vertex = dequeue from Q

for each neighbor in neighbors of current_vertex:

if neighbor is not visited:

mark neighbor as visited

enqueue neighbor into Q

distances[neighbor] = distances[current_vertex] + 1

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


Complexity Analysis 29

while Q is not empty:


current_vertex = dequeue from Q
for each neighbor in neighbors of current_vertex:
… … …

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


Depth-First Search 30
• Let’s see DFS now.
• We will try to go as deep as possible starting from a node.
• We won’t check other adjacent like BFS.

• If we can’t go any deeper we will come back to the previous node.


• We won’t visit any node twice.

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


Depth-First Search 31
• Consider the graph.
• To mark a node as visited 1
we will keep an array.
• Similar to BFS we will need a data structure. 2 3 4

• Instead of queue we will use stack now.

5 6 7

8 9

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


DFS Simulation 32
Store Check
the front
Weallelement
We the asofvisited
startadjacent
mark 1with theof
node stack
cur. in cur
1.
And Now we
If aremove
adjacent enter
it from a loop
the stack.
cur and push itisinto
unvisited,
stack.
mark it and push it in stack. 1
3
4
1
7
2 3 4
Stack

4
9
1
7 3
6 2

5 6 7
Visited Array

0 1 2 3 4 5 6 7 8 9
8 9
0 0
1 1
0 1
0 0
1 0 0
1 0
1 0 1
0

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


DFS Simulation 33
Store Check
the front
allelement
the adjacent of theof stack
cur. in cur
And The
If aremove Stack
adjacent isis unvisited,
it from Empty.
the stack.
cur
markSoit we
andstop
push the loop.
it in stack. 1
9
8
2
3
6
5
4
1
7
2 3 4
Stack

9 56
8 2

5 6 7
Visited Array

0 1 2 3 4 5 6 7 8 9
8 9
0 1 1 1 1 0
1 1 1 0
1 1

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


DFS Pseudocode 34
function DFS_with_Stack(graph, start_vertex):

create an empty stack S and push the start_vertex onto S

create a array visited to store visited vertices

mark start_vertex as visited

while S is not empty:

current_vertex = pop from S

for each neighbor in neighbors of current_vertex:

if neighbor is not in visited:

mark neighbor as visited

push neighbor onto S

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


Simplifying DFS 35
• Can we perform the DFS without using the built-in stack?
• We know that compilers maintain a implicit stack to handle recursion.
• So let’s convert our DFS function into a recursive one.

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


DFS with Recursion Pseudocode 36
function DFS(graph, current_vertex, visited):
if current_vertex is not in visited:
mark current_vertex as visited
for each neighbor in neighbors of current_vertex:
if neighbor is not visited:
DFS(graph, neighbor, visited)

Which implementation is better? Iterative or Recursive?

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


Complexity Analysis 37

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


BFS/DFS on Disconnected Graph 38
• A graph is said to be connected if there exists at least one path between
each and every pair of vertices in graph G, otherwise, it is disconnected.
• How can you traverse all the nodes in a disconnected graph?
• Run a BFS/DFS from each unvisited node.

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


BFS/DFS 39

12
1 14 9

10
13
5
2 8
15
3
7
11
4
6

CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024


CSE 2106: Data Structures and Algorithms Laboratory 2/8/2024 40

Thank You.

You might also like