bcs401 Sample Report
bcs401 Sample Report
2024 – 2025
Pedagogy Report
On
Submitted in partial fulfilment of the requirements for the award of the degree of
Bachelor of Engineering
in
Computer Science and Engineering
Submitted by
1
TABLE OF CONTENTS:
01 Introduction 3
02 Problem Definitions 3
9
04 Warshall’s Algorithm 7-9
07 Conclusion 10
2
Design and Implementation of Floyd’s and Warshall’s Algorithms in
C++
Introduction
Graph theory is foundational to many fields such as computer networks, databases, compiler
construction, transportation, and AI. Algorithms to compute shortest paths and reachability are core
to these applications. This report focuses on two such algorithms — Floyd’s Algorithm, which
computes the shortest paths between all vertex pairs, and Warshall’s Algorithm, which determines
whether a path exists between any two vertices.
Both algorithms are particularly useful for dense graphs and are efficiently implemented using
adjacency matrices and dynamic programming.
Problem Definitions
In this report, we represent graphs using adjacency matrices — a 2D array where graph[i][j] stores
the weight of the edge from vertex i to j (or 0/1 for reachability).
In C++, we use:
• vector<vector<int>> from the Standard Template Library (STL)
• Constants like INT_MAX to represent infinity
3
Floyd’s Algorithm
Description
Floyd’s Algorithm (also known as Floyd–Warshall) solves the all-pairs shortest path problem. It is
based on dynamic programming and updates the shortest path between every pair (i, j) by considering
whether going through an intermediate vertex k offers a shorter route.
Working Principle
Example:
Given the graph:
(0)---3--->(1)
| |
5 4
| v
(3)<--2----(2)
Adjacency Matrix:
0 3 ∞ 5
2 0 ∞ 4
∞ 1 0 ∞
∞ ∞ 2 0
Algorithm
for (int k = 0; k < n; ++k)
4
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
if (dist[i][k] + dist[k][j] < dist[i][j])
dist[i][j] = dist[i][k] + dist[k][j];
Complexity Analysis
• Time Complexity: O(n³) — Three nested loops over the number of vertices.
• Space Complexity: O(n²) — For the distance matrix.
C++ Implementation
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
int main() {
vector<vector<int>> graph = {
{0, 3, INF, 5},
{2, 0, INF, 4},
{INF, 1, 0, INF},
{INF, INF, 2, 0}
};
floydWarshall(graph);
return 0;
}
Sample Output:
Shortest distances between every pair of vertices:
0375
2064
3105
5320
6
Warshall’s Algorithm
Description
Warshall’s algorithm computes the transitive closure of a directed graph — identifying reachability
from one vertex to another. It modifies the adjacency matrix such that if a path exists from vertex i
to j, then the final matrix will have a 1 at matrix[i][j].
Working Principle
For each vertex k:
if path[i][j] is not 1,
and path[i][k] and path[k][j] are both 1,
then set path[i][j] = 1.
Example:
Input Graph:
010
001
100
Transitive Closure:
111
111
111
Algorithm
for (int k = 0; k < n; ++k)
for (int i = 0; i < n; ++i)
7
for (int j = 0; j < n; ++j)
path[i][j] = path[i][j] || (path[i][k] && path[k][j]);
Complexity Analysis
• Time Complexity: O(n³)
• Space Complexity: O(n²)
C++ Implementation
#include <iostream>
#include <vector>
using namespace std;
#define V 3
int main() {
8
vector<vector<int>> graph = {
{0, 1, 0},
{0, 0, 1},
{1, 0, 0}
};
warshall(graph);
return 0;
}
Sample Output
Transitive closure of the given graph:
111
111
111
• Floyd's Algorithm:
o Network routing and link-state protocols.
o Urban transportation modeling.
o Game AI pathfinding for all agents.
o Optimal message passing in social networks.
• Warshall's Algorithm:
o Database systems: query reachability in graphs.
o Compiler design: control-flow and data-flow analysis.
o Web graph crawling and link analysis.
9
Advantages and Limitations
Limitations:
• Inefficient for very large sparse graphs — prefer Dijkstra or DFS-based approaches.
• Floyd’s algorithm doesn’t work with negative cycles.
Conclusion
This report demonstrated how to implement two classical graph algorithms — Floyd’s and Warshall’s
— using C++. By leveraging adjacency matrices and dynamic programming, both algorithms solve
important problems related to shortest path and reachability. Their O(n³) complexity makes them
suitable for dense graphs and academic research, while real-world applications extend to domains
such as network routing, artificial intelligence, and compiler optimization.
10
11