Comparison between different graph algorithms

Comprehensive study notes, diagrams, and exam preparation for Comparison between different graph algorithms.

Comparison between Different Graph Algorithms

Definition

A comparison between different graph algorithms is the study of how graph algorithms differ in terms of purpose, working principle, time complexity, space complexity, graph type suitability, and practical applications.

Graph algorithms are methods used to process graph data structures. A graph consists of vertices (nodes) and edges (connections). Depending on the problem, one may use traversal algorithms like BFS or DFS, shortest path algorithms like Dijkstra or Bellman-Ford, minimum spanning tree algorithms like Prim or Kruskal, or specialized algorithms like topological sort and Floyd-Warshall. Comparing them helps determine which algorithm is most suitable for a given scenario.


Main Content

1. Graph Traversal Algorithms: BFS and DFS

Breadth First Search (BFS)

  • explores a graph level by level, visiting all neighboring vertices before moving deeper. It is especially useful in unweighted graphs for finding the shortest path in terms of number of edges. Example: In a social network, BFS can find all friends at distance 1, then 2, then 3 from a user.

Working idea: BFS uses a queue. Starting from a source node, it visits all adjacent nodes first, then their neighbors, and so on. This level-wise exploration makes BFS ideal for shortest path in unweighted graphs and for finding the minimum number of edges between two nodes.

Time complexity: O(V + E) where V is vertices and E is edges.

Depth First Search (DFS)

  • explores as far as possible along one branch before backtracking. It is useful for cycle detection, connected components, topological sorting, and path existence checks. Example: In a maze, DFS goes deep into one route first and backtracks when it reaches a dead end.

Working idea: DFS typically uses a stack or recursion. It follows one path deeply before returning and exploring other paths. This makes DFS very effective for tasks that require exhaustive exploration of paths or recursive structure.

Time complexity: O(V + E).

Comparison of BFS and DFS

  • BFS is better for shortest path in unweighted graphs; DFS is better for deep exploration and backtracking problems.
  • BFS uses more memory in wide graphs because it stores a whole frontier; DFS usually uses less memory but may go deep into recursion.
  • BFS is level-oriented; DFS is path-oriented.

Simple example graph:

A --- B --- D
|     |
C     E

Starting from A:

  • BFS order may be: A, B, C, D, E
  • DFS order may be: A, B, D, E, C
    (exact DFS order depends on adjacency order)

2. Shortest Path Algorithms: Dijkstra, Bellman-Ford, and Floyd-Warshall

Dijkstra’s Algorithm

  • finds the shortest path from a single source to all other vertices in a graph with non-negative edge weights. Example: Finding the shortest driving route where all road distances are positive.

Working idea: It greedily selects the vertex with the smallest known distance, then relaxes its adjacent edges. A priority queue often improves efficiency.

Time complexity:

  • With simple array: O(V^2)
  • With priority queue: O((V + E) log V)

Limitation: It does not work correctly with negative edge weights.

Bellman-Ford Algorithm

  • also finds the shortest path from a single source, but it can handle negative edge weights and detect negative cycles. Example: Cost calculations where some edges represent discounts or rebates.

Working idea: It repeatedly relaxes all edges V-1 times. If a further relaxation is possible after that, a negative cycle exists.

Time complexity: O(VE)

Advantage: Works with negative weights and can detect negative cycles.

Floyd-Warshall Algorithm

  • computes shortest paths between all pairs of vertices. Example: Finding the shortest travel time between every pair of cities in a transportation network.

Working idea: It uses dynamic programming and updates shortest paths by considering each vertex as an intermediate node.

Time complexity: O(V^3)
Space complexity: O(V^2)

Comparison among shortest path algorithms

  • Dijkstra is fastest for single-source shortest paths on graphs with non-negative weights.
  • Bellman-Ford is slower but handles negative weights and detects negative cycles.
  • Floyd-Warshall is suitable for all-pairs shortest paths, especially in smaller graphs.

Small weighted graph example:

A --4--> B --2--> C
A --1--> D --5--> C

From A to C:

  • A -> B -> C = 6
  • A -> D -> C = 6
  • A -> D then D -> C may be chosen depending on edge direction/weights

3. Minimum Spanning Tree Algorithms: Prim’s and Kruskal’s

Minimum Spanning Tree (MST)

  • is a subset of edges connecting all vertices with minimum total weight and no cycles. Example: Designing the cheapest network of cables to connect all offices.

Prim’s Algorithm

  • grows a single tree starting from any vertex and repeatedly adds the minimum-weight edge that connects the tree to a new vertex. Working idea: It maintains a set of visited vertices and chooses the smallest edge crossing the cut between visited and unvisited vertices.

Time complexity:

  • With adjacency matrix: O(V^2)
  • With priority queue: O(E log V)

Best for: Dense graphs.

Kruskal’s Algorithm

  • sorts all edges by weight and adds them one by one if they do not create a cycle. Working idea: It uses a Disjoint Set Union (DSU/Union-Find) structure to detect cycles efficiently.

Time complexity: O(E log E)

Best for: Sparse graphs.

Comparison of Prim and Kruskal

  • Prim starts from a vertex and expands the tree; Kruskal starts with edges and builds the forest.
  • Prim is often better for dense graphs; Kruskal is often better for sparse graphs.
  • Kruskal requires sorting edges and cycle detection using DSU.

Example weighted graph:

   2
A------B
| \    |
3|  \1 |4
|   \  |
C------D
    5

An MST will choose the minimum edges that connect all vertices without cycles.


Working / Process

1. Identify the graph problem type

Determine whether the problem is about traversal, shortest path, minimum spanning tree, cycle detection, connectivity, or ordering.
For example:

  • Use BFS/DFS for exploration.
  • Use Dijkstra/Bellman-Ford/Floyd-Warshall for shortest paths.
  • Use Prim/Kruskal for MST.

2. Check graph properties

Analyze whether the graph is directed or undirected, weighted or unweighted, sparse or dense, and whether negative weights are present.
This step is essential because algorithm suitability depends on these properties:

  • BFS works well in unweighted graphs.
  • Dijkstra requires non-negative weights.
  • Bellman-Ford handles negative weights.
  • Prim/Kruskal are used on weighted undirected graphs for MST.

3. Select and apply the most suitable algorithm

Choose the algorithm based on correctness and efficiency. Then execute its process:

  • BFS: use queue and level-order traversal.
  • DFS: use recursion or stack.
  • Dijkstra: repeatedly choose the nearest unvisited vertex and relax edges.
  • Bellman-Ford: relax all edges repeatedly.
  • Prim: add minimum edge from the current tree.
  • Kruskal: sort edges and add safe edges using DSU.

Advantages / Applications

Efficient problem solving

Graph algorithms provide systematic methods to solve complex real-world problems such as navigation, networking, scheduling, and dependency resolution.

Proper algorithm selection improves performance

Comparing algorithms helps choose the most efficient one for a given graph type. For example, BFS is better than Dijkstra for shortest paths in unweighted graphs, and Prim may be better than Kruskal for dense graphs.

Wide practical applications

Graph algorithms are used in:

  • GPS and route planning
  • Social network analysis
  • Network routing
  • Compiler design
  • Web crawling and search engines
  • Minimum-cost infrastructure design
  • Task scheduling and dependency management

Summary

  • Graph algorithms are compared based on their purpose, graph type suitability, and efficiency.
  • BFS and DFS are traversal algorithms, while Dijkstra, Bellman-Ford, and Floyd-Warshall are shortest path algorithms; Prim and Kruskal are MST algorithms.
  • BFS is ideal for unweighted shortest paths, Dijkstra for non-negative weighted shortest paths, Bellman-Ford for negative weights, and Prim/Kruskal for building minimum spanning trees.