SlideShare a Scribd company logo
University of Science &
Technology , Chittagong
Submitted to Submitted by
Sukanta Paul Name: PIKU DAS
Lecturer Roll No: 17010110
Department of CSE Batch No:29th
Graph Traverse
• Graph traversal means visiting every vertex and edge exactly once in a
well-defined order. While using certain graph algorithms, you must
ensure that each vertex of the graph is visited exactly once.
During a traversal , it is important that you track which vertices
have been Visited . The most common way of tracking vertices is to
mark them
Breadth First Search(BFS)
• There are many ways to traverse graphs . BFS is the most commonly
used approach .
BFS is a traversing algorithm where you should start traversing from a
selected node and traverse the graph layer-wise or you should visit
both Children of a node.
Task
• Our task is to write a procedure with input City Denver to City
Washington from the given Link Representation which will find the
shortest stops.
The Link Representation is given below
Link Representation
CITY NO CITY LEFT RIGHT ADJ
1 Atlanta 0 2 12
2 Boston 0 0 1
3 Houston 0 0 14
4 Ney York 3 8 4
5 6
6 0
7 Washington 0 0 10
8 Philadelphia 0 7 6
9 Denver 10 4 8
10 Chicago 1 0 2
Link Representation
ADJ STOP NO PRICE ORIG DEST LINK
1 201 80 2 10 3
2 202 80 10 2 0
3 301 50 2 4 0
4 302 50 4 2 5
5 303 40 4 8 7
6 304 40 8 4 9
7 305 120 4 9 0
8 306 120 9 4 13
9 401 40 8 7 0
10 402 40 7 8 11
Link Representation
ADJ STOP NO PRICE ORIG DEST LINK
11 403 80 7 1 0
12 404 80 1 7 16
13 501 80 9 3 15
14 502 80 3 9 0
15 503 140 9 1 0
16 504 140 1 9 0
17 18
18 19
19 20
20 0
Search Process
• Here we want the minimum number of stops between Denver to
Washington .
• This can be found by using BFS process beginning at Denver and
ending when Washington is encountered.
• From the given Link Representation we can draw a graph which will
notify the path of Denver to Washington , and their adjacency.
Graph
Here
Denver
Chicago New York
Atlanta Houston Philadelphia
Boston Washington
Adjacency
CITY ADJ
Denver New York ,Houston , Atlanta
Chicago Boston
Atlanta Washington , Denver
Boston Chicago , New York
New York Boston , Philadelphia , Denver
Houston Denver
Philadelphia New York ,Washington
Washington Philadelphia ,Atlanta
Steps
• Here we will use a data structure called Queue , where insertion
starts in Rear and deletion in Front.
• We will also keep track the origin of each node using an array called
origin.
• Step 1:
Initially add city Denver to Queue and add null to orig
Front =1 Queue : Denver
Rear =1 Origin : 0
Steps
• Step 2:
Remove the front element Denver and set
Front = Front + 1,
And add t0 Queue the child of Denver.
Front:2 Queue : Denver, New
York ,Houston,
Atlanta
Rear : 4 origin : 0,Denver,Denver
Denver
CITY ADJ
Denver New –York , Houston ,
Atlanta
Chicago Boston
Atlanta Washington , Denver
Boston Chicago , New York
New York Boston , Philadelphia ,
Denver
Houston Denver
Philadelphia New York ,Washington
Washington Philadelphia ,Atlanta
Steps
• Step 3:
Remove the front element New York and set
Front = Front + 2,
And add t0 Queue the child of New
York.
Front: 3 Queue : Denver, New
York ,Houston,
Atlanta , Boston , Philadelphia
Rear : 6 origin : 0,Denver,Denver
Denver , New York ,
New York
CITY ADJ
Denver New –York , Houston ,
Atlanta
Chicago Boston
Atlanta Washington , Denver
Boston Chicago , New York
New York Boston , Philadelphia ,
Denver
Houston Denver
Philadelphia New York ,Washington
Washington Philadelphia ,Atlanta
Steps
As child of Houston, Denver is already in
Queue , we need not add in Queue
Front: 4 Queue : Denver, New
York ,Houston,
Atlanta , Boston,
Philadelphia
Rear : 6 origin:0 , Denver
Denver, Denver, New-
York , New York
CITY ADJ
Denver New –York , Houston ,
Atlanta
Chicago Boston
Atlanta Washington , Denver
Boston Chicago , New York
New York Boston , Philadelphia ,
Denver
Houston Denver
Philadelphia New York ,Washington
Washington Philadelphia ,Atlanta
Steps
Step 4: Remove front element
Atlanta and add the child of Atlanta
Front: 4 Queue : Denver, New
York ,Houston,
Atlanta , Boston,
Philadelphia, Washington
Rear : 6 origin: 0 , Denver
Denver, Denver, New-
York , New York , Atlanta
CITY ADJ
Denver New –York , Houston ,
Atlanta
Chicago Boston
Atlanta Washington , Denver
Boston Chicago , New York
New York Boston , Philadelphia ,
Denver
Houston Denver
Philadelphia New York ,Washington
Washington Philadelphia ,Atlanta
• As Washington is added to the Queue we have to stop.
• We now backtrack from Washington using the array origin to find the
shortest stops
Washington Atlanta Denver
So this is the shortest way to get minimum stops from city Denver
to Washington.
Shortest Path
So
Denver
Chicago New York
Atlanta Houston Philadelphia
Boston Washington
Source Code
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#define maxVertices 100
• typedef struct Queue
{
int capacity;
int size;
int front;
int rear;
int *elements;
}Queue;
Queue * CreateQueue(int maxElements)
{
/* Create a Queue */
Queue *Q;
Q = (Queue *)malloc(sizeof(Queue));
/* Initialise its properties */
Q->elements = (int *)malloc(sizeof(int)*maxElements);
Q->size = 0;
Q->capacity = maxElements;
Q->front = 0;
Q->rear = -1;
/* Return the pointer */
return Q;
}
Source Code
void Dequeue(Queue *Q)
{
/* If Queue size is zero then it is empty. So we cannot pop */
if(Q->size==0)
{
printf("Queue is Emptyn");
return;
}
Source Code
else
{
Q->size--;
Q->front++;
/* As we fill elements in circular fashion */
if(Q->front==Q->capacity)
{
Q->front=0;
}
}
return;
}
Source Code
int Front(Queue *Q)
{
if(Q->size==0)
{
printf("Queue is Emptyn");
exit(0);
}
/* Return the element which is at the front*/
return Q->elements[Q->front];
}
Source Code
void Enqueue(Queue *Q,int element)
{
/* If the Queue is full, we cannot push an element into it as there
is no space for it.*/
if(Q->size == Q->capacity)
{
printf("Queue is Fulln");
}
Source Code
• else
{
Q->size++;
Q->rear = Q->rear + 1;
/* As we fill the queue in circular fashion */
if(Q->rear == Q->capacity)
{
Q->rear = 0;
}
/* Insert the element in its rear side */
Q->elements[Q->rear] = element;
}
return;
}
Source Code
void Bfs(int graph[][maxVertices], int *size, int presentVertex,int *visited)
{
visited[presentVertex] = 1;
/* Iterate through all the vertices connected to the presentVertex and perform bfs on those
vertices if they are not visited before */
Queue *Q = CreateQueue(maxVertices);
Enqueue(Q,presentVertex);
while(Q->size)
{
presentVertex = Front(Q);
printf("Now visiting vertex %dn",presentVertex);
Dequeue(Q);
Source Code
int iter;
for(iter=0;iter<size[presentVertex];iter++)
{
if(!visited[graph[presentVertex][iter]])
{
visited[graph[presentVertex][iter]] = 1;
Enqueue(Q,graph[presentVertex][iter]);
}
}
}
return;
}
Source Code
int main()
{ int
graph[maxVertices][maxVertices],size[maxVertices]={0},visited[maxVert
ices]={0};
int vertices,edges,iter;
/* vertices represent number of vertices and edges represent
number of edges in the graph. */
scanf("%d%d",&vertices,&edges);
int vertex1,vertex2;
Source Code
for(iter=0;iter<edges;iter++)
{
scanf("%d%d",&vertex1,&vertex2);
assert(vertex1>=0 && vertex1<vertices);
assert(vertex2>=0 && vertex2<vertices);
graph[vertex1][size[vertex1]++] = vertex2;
}
Source Code
int presentVertex;
for(presentVertex=0;presentVertex<vertices;presentVertex++)
{
if(!visited[presentVertex])
{
Bfs(graph,size,presentVertex,visited);
}
}
return 0;
Rough Notes about the Algorithm
• Input Format: Graph is directed and unweighted. First two integers
must be number of vertices and edges which must be followed by
pairs of vertices which has an edge between them.
• maxVertices represents maximum number of vertices that can be
present in the graph.
• vertices represent number of vertices and edges represent number of
edges in the graph.
Rough Notes about the Algorithm
graph[i][j] represent the weight of edge joining i and j.
size[maxVertices] is initialed to{0}, represents the size of every vertex
i.e. the number of
• edges corresponding to the vertex.
• visited[maxVertices]={0} represents the vertex that have been visited.
Rough Notes about the Algorithm
• The Queue has five properties -
• 1. createQueue function takes argument the maximum number of
elements the Queue can hold, creates a Queue according to it and
returns a pointer to the Queue. It initializes Q- >size to 0, Q->capacity
to maxElements, Q->front to 0 and Q->rear to -1.
• 2. enqueue function - This function takes the pointer to the top of the
queue Q and the item (element) to be inserted as arguments. Check
for the emptiness of queue
Rough Notes about the Algorithm
• a. If Q->size is equal to Q->capacity, we cannot push an element into
Q as there is no space for it.
• b. Else, enqueue an element at the end of Q, increase its size by one.
Increase the value of Q->rear to Q->rear + 1. As we fill the queue in
circular fashion, if Q->rear is equal to Q->capacity make Q->rear = 0.
Now, Insert the element in its rear side Q>elements[Q->rear] =
element.
Rough Notes about the Algorithm
• 3. dequeue function - This function takes the pointer to the top of the
stack S as an argument.
If Q->size is equal to zero, then it is empty. So, we cannot dequeue.
Else, remove an element which is equivalent to incrementing index
of front by one. Decrease the size by 1. As we fill elements in circular
fashion, if Q->front is equal to Q->capacity make Q->front=0.
Rough Notes about the Algorithm
• 4. front function – This function takes the pointer to the top of the
queue Q as an argument and returns the front element of the queue
Q. It first checks if the queue is empty
• (Q->size is equal to zero). If it’s not it returns the element which is at
the front of the queue.
• Q->elements[Q->front]
Breadth first search
Breadth first search

More Related Content

Similar to Breadth first search

Data structures
Data structuresData structures
Data structures
Jauhar Amir
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Afaq Mansoor Khan
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptx
prakashvs7
 
Ai1.pdf
Ai1.pdfAi1.pdf
Ai1.pdf
kaxeca4096
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
SherinRappai
 
Data Structures and Algorithm AnalysisSpring 2020 Post Midterm E
Data Structures and Algorithm AnalysisSpring 2020 Post Midterm EData Structures and Algorithm AnalysisSpring 2020 Post Midterm E
Data Structures and Algorithm AnalysisSpring 2020 Post Midterm E
jeniihykdevara
 
Lec3
Lec3Lec3
Queue(lecture8).pptx
Queue(lecture8).pptxQueue(lecture8).pptx
Queue(lecture8).pptx
singhprpg
 
Queue
QueueQueue
Queues
QueuesQueues
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
Dabbal Singh Mahara
 
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparation
RAtna29
 
Circular queue
Circular queueCircular queue
Circular queue
Circular queueCircular queue
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures
LavanyaJ28
 
Data Structure - Stack.pptx
Data Structure - Stack.pptxData Structure - Stack.pptx
Data Structure - Stack.pptx
MarlonMagtibay2
 
non-predective parser
non-predective parsernon-predective parser
non-predective parser
TarjMehta1
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
tameemyousaf
 
Data Structures and Algorithm Analysis1. How much memory w.docx
Data Structures and Algorithm Analysis1. How much memory w.docxData Structures and Algorithm Analysis1. How much memory w.docx
Data Structures and Algorithm Analysis1. How much memory w.docx
randyburney60861
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
chandankumar364348
 

Similar to Breadth first search (20)

Data structures
Data structuresData structures
Data structures
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
DS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptxDS-UNIT 3 FINAL.pptx
DS-UNIT 3 FINAL.pptx
 
Ai1.pdf
Ai1.pdfAi1.pdf
Ai1.pdf
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
Data Structures and Algorithm AnalysisSpring 2020 Post Midterm E
Data Structures and Algorithm AnalysisSpring 2020 Post Midterm EData Structures and Algorithm AnalysisSpring 2020 Post Midterm E
Data Structures and Algorithm AnalysisSpring 2020 Post Midterm E
 
Lec3
Lec3Lec3
Lec3
 
Queue(lecture8).pptx
Queue(lecture8).pptxQueue(lecture8).pptx
Queue(lecture8).pptx
 
Queue
QueueQueue
Queue
 
Queues
QueuesQueues
Queues
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
 
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparation
 
Circular queue
Circular queueCircular queue
Circular queue
 
Circular queue
Circular queueCircular queue
Circular queue
 
Unit ii linear data structures
Unit ii linear data structures Unit ii linear data structures
Unit ii linear data structures
 
Data Structure - Stack.pptx
Data Structure - Stack.pptxData Structure - Stack.pptx
Data Structure - Stack.pptx
 
non-predective parser
non-predective parsernon-predective parser
non-predective parser
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
 
Data Structures and Algorithm Analysis1. How much memory w.docx
Data Structures and Algorithm Analysis1. How much memory w.docxData Structures and Algorithm Analysis1. How much memory w.docx
Data Structures and Algorithm Analysis1. How much memory w.docx
 
Stack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptxStack_Overview_Implementation_WithVode.pptx
Stack_Overview_Implementation_WithVode.pptx
 

Recently uploaded

Global Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headedGlobal Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headed
vikram sood
 
Challenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more importantChallenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more important
Sm321
 
End-to-end pipeline agility - Berlin Buzzwords 2024
End-to-end pipeline agility - Berlin Buzzwords 2024End-to-end pipeline agility - Berlin Buzzwords 2024
End-to-end pipeline agility - Berlin Buzzwords 2024
Lars Albertsson
 
State of Artificial intelligence Report 2023
State of Artificial intelligence Report 2023State of Artificial intelligence Report 2023
State of Artificial intelligence Report 2023
kuntobimo2016
 
A presentation that explain the Power BI Licensing
A presentation that explain the Power BI LicensingA presentation that explain the Power BI Licensing
A presentation that explain the Power BI Licensing
AlessioFois2
 
The Ipsos - AI - Monitor 2024 Report.pdf
The  Ipsos - AI - Monitor 2024 Report.pdfThe  Ipsos - AI - Monitor 2024 Report.pdf
The Ipsos - AI - Monitor 2024 Report.pdf
Social Samosa
 
University of New South Wales degree offer diploma Transcript
University of New South Wales degree offer diploma TranscriptUniversity of New South Wales degree offer diploma Transcript
University of New South Wales degree offer diploma Transcript
soxrziqu
 
Experts live - Improving user adoption with AI
Experts live - Improving user adoption with AIExperts live - Improving user adoption with AI
Experts live - Improving user adoption with AI
jitskeb
 
Influence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business PlanInfluence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business Plan
jerlynmaetalle
 
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
74nqk8xf
 
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
bopyb
 
Palo Alto Cortex XDR presentation .......
Palo Alto Cortex XDR presentation .......Palo Alto Cortex XDR presentation .......
Palo Alto Cortex XDR presentation .......
Sachin Paul
 
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
apvysm8
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
Timothy Spann
 
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Aggregage
 
一比一原版(Chester毕业证书)切斯特大学毕业证如何办理
一比一原版(Chester毕业证书)切斯特大学毕业证如何办理一比一原版(Chester毕业证书)切斯特大学毕业证如何办理
一比一原版(Chester毕业证书)切斯特大学毕业证如何办理
74nqk8xf
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
Timothy Spann
 
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
g4dpvqap0
 
The Building Blocks of QuestDB, a Time Series Database
The Building Blocks of QuestDB, a Time Series DatabaseThe Building Blocks of QuestDB, a Time Series Database
The Building Blocks of QuestDB, a Time Series Database
javier ramirez
 
一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理
aqzctr7x
 

Recently uploaded (20)

Global Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headedGlobal Situational Awareness of A.I. and where its headed
Global Situational Awareness of A.I. and where its headed
 
Challenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more importantChallenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more important
 
End-to-end pipeline agility - Berlin Buzzwords 2024
End-to-end pipeline agility - Berlin Buzzwords 2024End-to-end pipeline agility - Berlin Buzzwords 2024
End-to-end pipeline agility - Berlin Buzzwords 2024
 
State of Artificial intelligence Report 2023
State of Artificial intelligence Report 2023State of Artificial intelligence Report 2023
State of Artificial intelligence Report 2023
 
A presentation that explain the Power BI Licensing
A presentation that explain the Power BI LicensingA presentation that explain the Power BI Licensing
A presentation that explain the Power BI Licensing
 
The Ipsos - AI - Monitor 2024 Report.pdf
The  Ipsos - AI - Monitor 2024 Report.pdfThe  Ipsos - AI - Monitor 2024 Report.pdf
The Ipsos - AI - Monitor 2024 Report.pdf
 
University of New South Wales degree offer diploma Transcript
University of New South Wales degree offer diploma TranscriptUniversity of New South Wales degree offer diploma Transcript
University of New South Wales degree offer diploma Transcript
 
Experts live - Improving user adoption with AI
Experts live - Improving user adoption with AIExperts live - Improving user adoption with AI
Experts live - Improving user adoption with AI
 
Influence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business PlanInfluence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business Plan
 
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
 
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
 
Palo Alto Cortex XDR presentation .......
Palo Alto Cortex XDR presentation .......Palo Alto Cortex XDR presentation .......
Palo Alto Cortex XDR presentation .......
 
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
 
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
 
一比一原版(Chester毕业证书)切斯特大学毕业证如何办理
一比一原版(Chester毕业证书)切斯特大学毕业证如何办理一比一原版(Chester毕业证书)切斯特大学毕业证如何办理
一比一原版(Chester毕业证书)切斯特大学毕业证如何办理
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
 
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
 
The Building Blocks of QuestDB, a Time Series Database
The Building Blocks of QuestDB, a Time Series DatabaseThe Building Blocks of QuestDB, a Time Series Database
The Building Blocks of QuestDB, a Time Series Database
 
一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理
 

Breadth first search

  • 1. University of Science & Technology , Chittagong Submitted to Submitted by Sukanta Paul Name: PIKU DAS Lecturer Roll No: 17010110 Department of CSE Batch No:29th
  • 2. Graph Traverse • Graph traversal means visiting every vertex and edge exactly once in a well-defined order. While using certain graph algorithms, you must ensure that each vertex of the graph is visited exactly once. During a traversal , it is important that you track which vertices have been Visited . The most common way of tracking vertices is to mark them
  • 3. Breadth First Search(BFS) • There are many ways to traverse graphs . BFS is the most commonly used approach . BFS is a traversing algorithm where you should start traversing from a selected node and traverse the graph layer-wise or you should visit both Children of a node.
  • 4. Task • Our task is to write a procedure with input City Denver to City Washington from the given Link Representation which will find the shortest stops. The Link Representation is given below
  • 5. Link Representation CITY NO CITY LEFT RIGHT ADJ 1 Atlanta 0 2 12 2 Boston 0 0 1 3 Houston 0 0 14 4 Ney York 3 8 4 5 6 6 0 7 Washington 0 0 10 8 Philadelphia 0 7 6 9 Denver 10 4 8 10 Chicago 1 0 2
  • 6. Link Representation ADJ STOP NO PRICE ORIG DEST LINK 1 201 80 2 10 3 2 202 80 10 2 0 3 301 50 2 4 0 4 302 50 4 2 5 5 303 40 4 8 7 6 304 40 8 4 9 7 305 120 4 9 0 8 306 120 9 4 13 9 401 40 8 7 0 10 402 40 7 8 11
  • 7. Link Representation ADJ STOP NO PRICE ORIG DEST LINK 11 403 80 7 1 0 12 404 80 1 7 16 13 501 80 9 3 15 14 502 80 3 9 0 15 503 140 9 1 0 16 504 140 1 9 0 17 18 18 19 19 20 20 0
  • 8. Search Process • Here we want the minimum number of stops between Denver to Washington . • This can be found by using BFS process beginning at Denver and ending when Washington is encountered. • From the given Link Representation we can draw a graph which will notify the path of Denver to Washington , and their adjacency.
  • 9. Graph Here Denver Chicago New York Atlanta Houston Philadelphia Boston Washington
  • 10. Adjacency CITY ADJ Denver New York ,Houston , Atlanta Chicago Boston Atlanta Washington , Denver Boston Chicago , New York New York Boston , Philadelphia , Denver Houston Denver Philadelphia New York ,Washington Washington Philadelphia ,Atlanta
  • 11. Steps • Here we will use a data structure called Queue , where insertion starts in Rear and deletion in Front. • We will also keep track the origin of each node using an array called origin. • Step 1: Initially add city Denver to Queue and add null to orig Front =1 Queue : Denver Rear =1 Origin : 0
  • 12. Steps • Step 2: Remove the front element Denver and set Front = Front + 1, And add t0 Queue the child of Denver. Front:2 Queue : Denver, New York ,Houston, Atlanta Rear : 4 origin : 0,Denver,Denver Denver CITY ADJ Denver New –York , Houston , Atlanta Chicago Boston Atlanta Washington , Denver Boston Chicago , New York New York Boston , Philadelphia , Denver Houston Denver Philadelphia New York ,Washington Washington Philadelphia ,Atlanta
  • 13. Steps • Step 3: Remove the front element New York and set Front = Front + 2, And add t0 Queue the child of New York. Front: 3 Queue : Denver, New York ,Houston, Atlanta , Boston , Philadelphia Rear : 6 origin : 0,Denver,Denver Denver , New York , New York CITY ADJ Denver New –York , Houston , Atlanta Chicago Boston Atlanta Washington , Denver Boston Chicago , New York New York Boston , Philadelphia , Denver Houston Denver Philadelphia New York ,Washington Washington Philadelphia ,Atlanta
  • 14. Steps As child of Houston, Denver is already in Queue , we need not add in Queue Front: 4 Queue : Denver, New York ,Houston, Atlanta , Boston, Philadelphia Rear : 6 origin:0 , Denver Denver, Denver, New- York , New York CITY ADJ Denver New –York , Houston , Atlanta Chicago Boston Atlanta Washington , Denver Boston Chicago , New York New York Boston , Philadelphia , Denver Houston Denver Philadelphia New York ,Washington Washington Philadelphia ,Atlanta
  • 15. Steps Step 4: Remove front element Atlanta and add the child of Atlanta Front: 4 Queue : Denver, New York ,Houston, Atlanta , Boston, Philadelphia, Washington Rear : 6 origin: 0 , Denver Denver, Denver, New- York , New York , Atlanta CITY ADJ Denver New –York , Houston , Atlanta Chicago Boston Atlanta Washington , Denver Boston Chicago , New York New York Boston , Philadelphia , Denver Houston Denver Philadelphia New York ,Washington Washington Philadelphia ,Atlanta
  • 16. • As Washington is added to the Queue we have to stop. • We now backtrack from Washington using the array origin to find the shortest stops Washington Atlanta Denver So this is the shortest way to get minimum stops from city Denver to Washington.
  • 17. Shortest Path So Denver Chicago New York Atlanta Houston Philadelphia Boston Washington
  • 18. Source Code #include<stdio.h> #include<stdlib.h> #include<assert.h> #define maxVertices 100 • typedef struct Queue { int capacity; int size; int front; int rear; int *elements; }Queue;
  • 19. Queue * CreateQueue(int maxElements) { /* Create a Queue */ Queue *Q; Q = (Queue *)malloc(sizeof(Queue)); /* Initialise its properties */ Q->elements = (int *)malloc(sizeof(int)*maxElements);
  • 20. Q->size = 0; Q->capacity = maxElements; Q->front = 0; Q->rear = -1; /* Return the pointer */ return Q; }
  • 21. Source Code void Dequeue(Queue *Q) { /* If Queue size is zero then it is empty. So we cannot pop */ if(Q->size==0) { printf("Queue is Emptyn"); return; }
  • 22. Source Code else { Q->size--; Q->front++; /* As we fill elements in circular fashion */ if(Q->front==Q->capacity) { Q->front=0; } } return; }
  • 23. Source Code int Front(Queue *Q) { if(Q->size==0) { printf("Queue is Emptyn"); exit(0); } /* Return the element which is at the front*/ return Q->elements[Q->front]; }
  • 24. Source Code void Enqueue(Queue *Q,int element) { /* If the Queue is full, we cannot push an element into it as there is no space for it.*/ if(Q->size == Q->capacity) { printf("Queue is Fulln"); }
  • 25. Source Code • else { Q->size++; Q->rear = Q->rear + 1; /* As we fill the queue in circular fashion */ if(Q->rear == Q->capacity) { Q->rear = 0; } /* Insert the element in its rear side */ Q->elements[Q->rear] = element; } return; }
  • 26. Source Code void Bfs(int graph[][maxVertices], int *size, int presentVertex,int *visited) { visited[presentVertex] = 1; /* Iterate through all the vertices connected to the presentVertex and perform bfs on those vertices if they are not visited before */ Queue *Q = CreateQueue(maxVertices); Enqueue(Q,presentVertex); while(Q->size) { presentVertex = Front(Q); printf("Now visiting vertex %dn",presentVertex); Dequeue(Q);
  • 28. Source Code int main() { int graph[maxVertices][maxVertices],size[maxVertices]={0},visited[maxVert ices]={0}; int vertices,edges,iter; /* vertices represent number of vertices and edges represent number of edges in the graph. */ scanf("%d%d",&vertices,&edges); int vertex1,vertex2;
  • 29. Source Code for(iter=0;iter<edges;iter++) { scanf("%d%d",&vertex1,&vertex2); assert(vertex1>=0 && vertex1<vertices); assert(vertex2>=0 && vertex2<vertices); graph[vertex1][size[vertex1]++] = vertex2; }
  • 31. Rough Notes about the Algorithm • Input Format: Graph is directed and unweighted. First two integers must be number of vertices and edges which must be followed by pairs of vertices which has an edge between them. • maxVertices represents maximum number of vertices that can be present in the graph. • vertices represent number of vertices and edges represent number of edges in the graph.
  • 32. Rough Notes about the Algorithm graph[i][j] represent the weight of edge joining i and j. size[maxVertices] is initialed to{0}, represents the size of every vertex i.e. the number of • edges corresponding to the vertex. • visited[maxVertices]={0} represents the vertex that have been visited.
  • 33. Rough Notes about the Algorithm • The Queue has five properties - • 1. createQueue function takes argument the maximum number of elements the Queue can hold, creates a Queue according to it and returns a pointer to the Queue. It initializes Q- >size to 0, Q->capacity to maxElements, Q->front to 0 and Q->rear to -1. • 2. enqueue function - This function takes the pointer to the top of the queue Q and the item (element) to be inserted as arguments. Check for the emptiness of queue
  • 34. Rough Notes about the Algorithm • a. If Q->size is equal to Q->capacity, we cannot push an element into Q as there is no space for it. • b. Else, enqueue an element at the end of Q, increase its size by one. Increase the value of Q->rear to Q->rear + 1. As we fill the queue in circular fashion, if Q->rear is equal to Q->capacity make Q->rear = 0. Now, Insert the element in its rear side Q>elements[Q->rear] = element.
  • 35. Rough Notes about the Algorithm • 3. dequeue function - This function takes the pointer to the top of the stack S as an argument. If Q->size is equal to zero, then it is empty. So, we cannot dequeue. Else, remove an element which is equivalent to incrementing index of front by one. Decrease the size by 1. As we fill elements in circular fashion, if Q->front is equal to Q->capacity make Q->front=0.
  • 36. Rough Notes about the Algorithm • 4. front function – This function takes the pointer to the top of the queue Q as an argument and returns the front element of the queue Q. It first checks if the queue is empty • (Q->size is equal to zero). If it’s not it returns the element which is at the front of the queue. • Q->elements[Q->front]