Computer Science‎ > ‎

Algorithms: Graph Traversal - Breadth First Search (with C Program source code)




To go through the C program / source-code, scroll down to the end of this page


Breadth-First Search


Breadth-first search is one of the simplest algorithms for searching a graph. Given a graph and 
a distinguished source vertex, breadth-first search explores the edges of the graph to find every 
vertex reachable from source. It computes the distance (fewest number of edges) from source 
to all reachable vertices and produces a “breadth-first tree” with source vertex as root, which 
contains all such reachable vertices. It works on both directed and undirected graphs. This 
algorithm uses a first-in, first-out Queue Q to manage the vertices.





Analysis

Initializing takes O(V) time The queue operations take time of O(V) as enqueuing and dequeuing takes O(1) time. In scanning the adjacency list at most O(E) time is spent. Thus, the total running time of BFS is O(V + E).


Complete Tutorial with Examples :




Breadth First Search - C Program Source Code


#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
/* maxVertices represents maximum number of vertices that can be present in the graph. */
#define maxVertices   100
/*Queue has five properties. capacity stands for the maximum number of elements Queue can hold.
  Size stands for the current size of the Queue and elements is the array of elements. front is the
 index of first element (the index at which we remove the element) and rear is the index of last element
 (the index at which we insert the element) */

typedef struct Queue
{
       
int capacity;
       
int size;
       
int front;
       
int rear;
       
int *elements;
}Queue;
/* crateQueue 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. */

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;
}
void Dequeue(Queue *Q)
{
       
/* If Queue size is zero then it is empty. So we cannot pop */
       
if(Q->size==0)
       
{
                printf
("Queue is Empty\n");
               
return;
       
}
       
/* Removing an element is equivalent to incrementing index of front by one */
       
else
       
{
                Q
->size--;
                Q
->front++;
               
/* As we fill elements in circular fashion */
               
if(Q->front==Q->capacity)
               
{
                        Q
->front=0;
               
}
       
}
       
return;
}
int Front(Queue *Q)
{
       
if(Q->size==0)
       
{
                printf
("Queue is Empty\n");
                exit
(0);
       
}
       
/* Return the element which is at the front*/
       
return Q->elements[Q->front];
}
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 Full\n");
       
}
       
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;
}



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 %d\n",presentVertex);
               
Dequeue(Q);
               
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;
       

}
/* Input Format: Graph is directed and unweighted. First two integers must be number of vertces and edges
   which must be followed by pairs of vertices which has an edge between them. */

int main()
{
       
int graph[maxVertices][maxVertices],size[maxVertices]={0},visited[maxVertices]={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;
       
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;
       
}
       
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.
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.

Initialize the graph.
For presentVertex = 0 to vertices if visited[presentVertex] is 0, i.e. if the vertex has not been visited then call Bfs function. presentVertex represents the vertex that is being tackled.
Bfs function is called to get the shortest path.

Bfs function: This function takes the graph obtained (graph[ ][ maxVertices]), pointer to the array size and visited, and the presentValue as arguments.

visited[presentVertex] = 1 as the vertex has now been visited.
Iterate through all the vertices connected to the presentVertex and perform bfs on those vertices if they are not visited before.
Create a queue Q using createQueue function and enqueue the presentVertex using Enqueue function.
Until the Q is not empty i.e. Q->size ≠ 0
• Store the front element of Q using Front function in presentVertex
• Print the vertex that is being visited now, which is presentVertex
• Remove the element from the front of Q using Dequeue function
• For iter=0 to size[presentVertex] – 1

If (!visited[graph[presentVertex][iter]])

visited[graph[presentVertex][iter]] = 1

Enqueue(Q,graph[presentVertex][iter])

Iter + 1

This for loop visits every vertex that is adjacent to the presentVertex and has not
been visited yet. These vertices are then inserted in the Q using Enqueue function
and their visited status is updated to 1.

The Queue has five properties - capacity stands for the maximum number of elements Queue can
hold, Size stands for the current size of the Queue, elements is the array of elements, front is the
index of first element (the index at which we remove the element) and rear is the index of last
element (the index at which we insert the element). Functions on Queue

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
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

3. dequeue function - This function takes the pointer to the top of the stack S as an
argument.
a. If Q->size is equal to zero, then it is empty. So, we cannot dequeue.
b. 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.
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]


Related Tutorials (basic Graph Algorithms) :

 Depth First Search 

 Traversing through a graph using Depth First Search in which unvisited neighbors of the current vertex are pushed into a stack and visited in that order. Breadth First Search 

 Traversing through a graph using Breadth First Search in which unvisited neighbors of the current vertex are pushed into a queue and then visited in that order. Minimum Spanning Trees: Kruskal Algorithm

 Finding the Minimum Spanning Tree using the Kruskal Algorithm which is a greedy technique. Introducing the concept of Union Find. Minumum Spanning Trees: Prim's Algorithm

 Finding the Minimum Spanning Tree using the Prim's Algorithm.  Dijkstra Algorithm for Shortest Paths

 Popular algorithm for finding shortest paths : Dijkstra Algorithm. Floyd Warshall Algorithm for Shortest Paths

 All the all shortest path algorithm: Floyd Warshall Algorithm Bellman Ford Algorithm 

 Another common shortest path algorithm : Bellman Ford Algorithm.




Recommended books for learning Computer Science, learning high quality programming, and preparing for programming interviews:

These are the standard sources of the knowledge expected from candidates interviewing at Google, Microsoft, Facebook, Amazon and other startups and top-tier technology companies.


 1. A good knowledge of Data Structures, Algorithm Design and Analysis of Complexity
The books by Cormen or Sedgewick (a standard part of the undergraduate curriculum) are sufficient for this part of the preparation. Google specially, loves to focus on algorithmic questions.
 Introduction to Algorithms (CLRS)  Algorithms (By Robert Sedgewick)    Analysis of Algorithms (By Robert Sedgewick)   
 2. Standard and established sources of Programming Interview Questions
A lot of interview questions are re-used and re-cycled by interviews again and again. Chances are, that question you're going to be asked, has been asked a hundred times during the last week, across a number of companies!
Google, Facebook, Microsoft, Amazon- all of them frequently ask questions on the lines of those in these books.

  Cracking the Coding Interview: 150 Programming Questions and Solutions [Paperback]    Programming Interviews Exposed    Ace the Programming Interview    Ace the IT Interview 
 3. Puzzles and Brainteasers (often asked, good to be prepared)
Questions from Mount Fuji are specially popular with Microsoft recruiters.

 

How would you move Mount Fuji?
 How to Ace the Brain Teaser Interview    
 4. Good Software Engineering practices. 
Writing clean and secure code, an awareness about Design Patterns and Object Oriented Programming techniques. Some of these books are frequently recommended by Microsoft recruiters. Take care of buffer overflows!
Code Complete is a recommended book for Microsoft Interviews.
 Code Complete 2   Writing Secure Code    Design Patterns  
 In case your primary language is C++, make sure you know its "ins-and-outs". You should know about destructors and constructors, virtual destructors, polymorphism, function overloading. Some of these books might help you revise C++.  The C++ Programming Language (Bjarne Stroustrup)  Beginning C++ through Game Programming   C++: How to Program (Deitel and Deitel)  
 In case your primary language is C, make sure to be well versed with pointers, system calls and how they work. How does Malloc work, how does calloc work? All of these are questions which some interviewers could potentially ask, to test the depth of your knowledge and your understanding of the system "under the hood".  The C Programming Language (Kernighan)  C How to Program (Deitel and Deitel)    
Here's a great book for the Java lovers as well.
It normally doesn't matter which programming language you know, but make sure to know about it in detail.
 Java: A Beginner's Guide (Herb Schildt)      



Testing Zone For Programmers- 

Try out our online Multiple-Choice-Question tests in Programming and Computer Science!



Photo-credits: www.istockphoto.com

 Quizzes on Basic Object Oriented Programming with C++   Quizzes on Java Programming
 Quizzes on C Programming- Arrays, Strings and Pointers
  1. C Programming MCQ Quiz #1:  Strings- 1
  2. C Programming MCQ Quiz #2: Strings (2)
  3. C Programming MCQ Quiz #3: Strings (3)
  4. C Programming MCQ Quiz #4: Arrays(1)
  5. C Programming MCQ Quiz #5: Arrays (2)
  6. C Programming MCQ Quiz #6: Arrays (3)
  7. C Programming MCQ Quiz #7: Pointers (1)
  8. C Programming MCQ Quiz #8: Pointers (2)
 Quizzes on Data Structures, Algorithms and Complexity
  1. MCQ Quiz #1: The Basics of Sorting Algorithms- Quadratic Sorts
  2. MCQ Quiz #2: Efficient Sorting Algorithms- Quick sort, Merge Sort, Heap Sort
  3. MCQ Quiz #3- The Radix Sort
  4. MCQ Quiz #4: Divide and Conquer Techniques- Binary Search, Quicksort, Merge sort, Complexities
  5. MCQ Quiz #5- Dynamic Programming
  6. MCQ Quiz #6- Complexity of Algorithms
  7. MCQ Quiz #7- Application of Master's Theorem
  8. MCQ Quiz #8: Binary Search Trees
  9. MCQ Quiz #9: B-Trees
  10. 10 MCQ Quiz #9: AVL-Trees
  11. 11 MCQ Quiz #10: Representing Graphs as Data Structures
  12. 12 MCQ Quiz #11: Spanning Trees
  13. 13 MCQ Quiz #12: Algorithms - Graphs: Spanning Trees - Kruskal and Prim Algorithms
  14. 14 MCQ Quiz #13: Algorithms - Graphs: Depth and Breadth First Search



Some Important Data Structures and Algorithms, at a glance:

Arrays : Popular Sorting and Searching Algorithms

 

  

Bubble Sort  

Insertion Sort 

Selection Sort Shell Sort

Merge Sort  

Quick Sort 

 
Heap Sort
 
Binary Search Algorithm

Basic Data Structures  and Operations on them


  

Stacks 

Queues  

 
 Single Linked List 

Double Linked List

Circular Linked List 










Basic Data Structures and Algorithms



Sorting- at a glance

 Bubble Sort One of the most elementary sorting algorithms to implement - and also very inefficient. Runs in quadratic time. A good starting point to understand sorting in general, before moving on to more advanced techniques and algorithms. A general idea of how the algorithm works and a the code for a C program.

Insertion Sort - Another quadratic time sorting algorithm - an example of dynamic programming. An explanation and step through of how the algorithm works, as well as the source code for a C program which performs insertion sort.

Selection Sort - Another quadratic time sorting algorithm - an example of a greedy algorithm. An explanation and step through of how the algorithm works, as well as the source code for a C program which performs selection sort.

Shell Sort- An inefficient but interesting algorithm, the complexity of which is not exactly known.

Merge Sort An example of a Divide and Conquer algorithm. Works in O(n log n) time. The memory complexity for this is a bit of a disadvantage.

Quick Sort In the average case, this works in O(n log n) time. No additional memory overhead - so this is better than merge sort in this regard. A partition element is selected, the array is restructured such that all elements greater or less than the partition are on opposite sides of the partition. These two parts of the array are then sorted recursively.

Heap Sort- Efficient sorting algorithm which runs in O(n log n) time. Uses the Heap data structure.

Binary Search Algorithm- Commonly used algorithm used to find the position of an element in a sorted array. Runs in O(log n) time.



Basic Data Structures and Algorithms


 Stacks Last In First Out data structures ( LIFO ). Like a stack of cards from which you pick up the one on the top ( which is the last one to be placed on top of the stack ). Documentation of the various operations and the stages a stack passes through when elements are inserted or deleted. C program to help you get an idea of how a stack is implemented in code.

Queues First in First Out data structure (FIFO). Like people waiting to buy tickets in a queue - the first one to stand in the queue, gets the ticket first and gets to leave the queue first. Documentation of the various operations and the stages a queue passes through as elements are inserted or deleted. C Program source code to help you get an idea of how a queue is implemented in code.

Single Linked List A self referential data structure. A list of elements, with a head and a tail; each element points to another of its own kind.

Double Linked List- A self referential data structure. A list of elements, with a head and a tail; each element points to another of its own kind in front of it, as well as another of its own kind, which happens to be behind it in the sequence.

Circular Linked List Linked list with no head and tail - elements point to each other in a circular fashion.

 Binary Search Trees A basic form of tree data structures. Inserting and deleting elements in them. Different kind of binary tree traversal algorithms.

 Heaps A tree like data structure where every element is lesser (or greater) than the one above it. Heap formation, sorting using heaps in O(n log n) time.

 Height Balanced Trees - Ensuring that trees remain balanced to optimize complexity of operations which are performed on them.

Graphs

 Depth First Search - Traversing through a graph using Depth First Search in which unvisited neighbors of the current vertex are pushed into a stack and visited in that order.

Breadth First Search - Traversing through a graph using Breadth First Search in which unvisited neighbors of the current vertex are pushed into a queue and then visited in that order.

Minimum Spanning Trees: Kruskal Algorithm- Finding the Minimum Spanning Tree using the Kruskal Algorithm which is a greedy technique. Introducing the concept of Union Find.

Minumum Spanning Trees: Prim's Algorithm- Finding the Minimum Spanning Tree using the Prim's Algorithm.

Dijkstra Algorithm for Shortest Paths- Popular algorithm for finding shortest paths : Dijkstra Algorithm.

Floyd Warshall Algorithm for Shortest Paths- All the all shortest path algorithm: Floyd Warshall Algorithm

Bellman Ford Algorithm - Another common shortest path algorithm : Bellman Ford Algorithm.

Dynamic Programming A technique used to solve optimization problems, based on identifying and solving sub-parts of a problem first.

Integer Knapsack problemAn elementary problem, often used to introduce the concept of dynamic programming.

Matrix Chain Multiplication Given a long chain of matrices of various sizes, how do you parenthesize them for the purpose of multiplication - how do you chose which ones to start multiplying first?

Longest Common Subsequence Given two strings, find the longest common sub sequence between them.

 Elementary cases : Fractional Knapsack Problem, Task Scheduling - Elementary problems in Greedy algorithms - Fractional Knapsack, Task Scheduling. Along with C Program source code.

Data Compression using Huffman TreesCompression using Huffman Trees. A greedy technique for encoding information.