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
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.
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.
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;
}
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;
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]