Introduction
Single source shortest path for undirected graph is basically the breadth first traversal of the graph. Here the graph we consider is unweighted and hence the shortest path would be the number of edges it takes to go from source to destination. You can find posts on the same topic for weighted graphs, and that is solved using Dijkstra's or Bellman Ford algorithms. This post is written from the competitive programming perspective. Here I want to focus on the details of simplified implementations.
Problem Statement - Shortest Path for Undirected Graph
Most of the times, the probl...

Read More
# Graph Traversals

## Euler Graphs – Origin of Graph Theory

This should have been my first post in the Graph Theory series but nevertheless I got time to discuss this now. Every one must have heard the famous problem of Seven Bridges of Königsberg. If not, then please take some time to read about the problem either on the Wikipedia or right down below:
The city of Konigsberb is located on both the banks of the river Pregel(Kaliningrad, Russia - former Prussia). The city also included two big islands and these islands were connected to each other and the main land by the means of seven bridges. Something like below:
The problem is to devise a...

Read More
## Single Source Shortest Path – Directed Acyclic Graph

[nextpage title="Introduction"]
Directed Acyclic Graph is a very special graph and has the following properties:
The edges of this graph are directed, it means all the edges have a designated direction.
There is no cycle in the graph, which means that a path will never contain one vertex more than once.
We referred to these kind of graphs in the topological ordering post.
Here we discuss the algorithm to find single source shortest path in such graphs. The good part is that unlike Dijkstra and Bellman Ford this can be solved in linear time O(E+V).
This algorithm for finding sho...

Read More
## Topological Ordering for Graphs

Introduction
The topological ordering for graphs get many applications where the nature of the problem is ordered sequential processing. Few of the problems in this category are as follows:
Dynamic Linking/Loading of programs while building and execution.
Deciding the pre-requisites in a course structure.
Job Scheduling in Processors or Assembly Line Processing.
Building an Alien Dictionary from given words.
There are many more but I am sure you got the idea. When we say topological ordering of graph, we are necessarily talking about the ordering of the vertices of the graph.
Un...

Read More
## Shortest Path Using Bellman Ford Algorithm

Introduction
This post about Bellman Ford Algorithm is a continuation of the post Shortest Path Using Dijkstra's Algorithm. While learning about the Dijkstra's way, we learnt that it is really efficient an algorithm to find the single source shortest path in any graph provided it has no negative weight edges and no negative weight cycles.
The running time of the Dijkstra's Algorithm is also promising, O(E +VlogV) depending on our choice of data structure to implement the required Priority Queue.
Why Bellman Ford Algorithm?
There can be scenarios where a graph may contain negative weight cycle...

Read More