SlideShare a Scribd company logo
1 of 38
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 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
 
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

Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
nirzagarg
 
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Reconciling Conflicting Data Curation Actions: Transparency Through Argument...
Reconciling Conflicting Data Curation Actions:  Transparency Through Argument...Reconciling Conflicting Data Curation Actions:  Transparency Through Argument...
Reconciling Conflicting Data Curation Actions: Transparency Through Argument...
Bertram Ludäscher
 
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
nirzagarg
 
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
vexqp
 
如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样
如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样
如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样
wsppdmt
 
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
nirzagarg
 
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
nirzagarg
 
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
q6pzkpark
 
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
nirzagarg
 
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
gajnagarg
 
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
nirzagarg
 
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
vexqp
 
Lecture_2_Deep_Learning_Overview-newone1
Lecture_2_Deep_Learning_Overview-newone1Lecture_2_Deep_Learning_Overview-newone1
Lecture_2_Deep_Learning_Overview-newone1
ranjankumarbehera14
 
Abortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get CytotecAbortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Riyadh +966572737505 get cytotec
 
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
gajnagarg
 

Recently uploaded (20)

Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
 
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Surabaya ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Reconciling Conflicting Data Curation Actions: Transparency Through Argument...
Reconciling Conflicting Data Curation Actions:  Transparency Through Argument...Reconciling Conflicting Data Curation Actions:  Transparency Through Argument...
Reconciling Conflicting Data Curation Actions: Transparency Through Argument...
 
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
 
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
怎样办理圣路易斯大学毕业证(SLU毕业证书)成绩单学校原版复制
 
如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样
如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样
如何办理英国诺森比亚大学毕业证(NU毕业证书)成绩单原件一模一样
 
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24  Building Real-Time Pipelines With FLaNKDATA SUMMIT 24  Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
 
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In Begusarai [ 7014168258 ] Call Me For Genuine Models...
 
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Hapur [ 7014168258 ] Call Me For Genuine Models We ...
 
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
一比一原版(曼大毕业证书)曼尼托巴大学毕业证成绩单留信学历认证一手价格
 
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Tumkur [ 7014168258 ] Call Me For Genuine Models We...
 
Switzerland Constitution 2002.pdf.........
Switzerland Constitution 2002.pdf.........Switzerland Constitution 2002.pdf.........
Switzerland Constitution 2002.pdf.........
 
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
 
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
 
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
怎样办理圣地亚哥州立大学毕业证(SDSU毕业证书)成绩单学校原版复制
 
Lecture_2_Deep_Learning_Overview-newone1
Lecture_2_Deep_Learning_Overview-newone1Lecture_2_Deep_Learning_Overview-newone1
Lecture_2_Deep_Learning_Overview-newone1
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
Sequential and reinforcement learning for demand side management by Margaux B...
Sequential and reinforcement learning for demand side management by Margaux B...Sequential and reinforcement learning for demand side management by Margaux B...
Sequential and reinforcement learning for demand side management by Margaux B...
 
Abortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get CytotecAbortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get Cytotec
 
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
Top profile Call Girls In bhavnagar [ 7014168258 ] Call Me For Genuine Models...
 

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]