0% found this document useful (0 votes)
7 views11 pages

bcs401 Sample Report

This report presents the analysis and design of Floyd's and Warshall's algorithms, focusing on their implementation in C++. Floyd's algorithm computes the shortest paths between all vertex pairs in a weighted graph, while Warshall's algorithm determines the reachability between vertices using boolean logic. Both algorithms are efficient for dense graphs and have applications in various fields such as network routing and database systems.

Uploaded by

megha200520
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views11 pages

bcs401 Sample Report

This report presents the analysis and design of Floyd's and Warshall's algorithms, focusing on their implementation in C++. Floyd's algorithm computes the shortest paths between all vertex pairs in a weighted graph, while Warshall's algorithm determines the reachability between vertices using boolean logic. Both algorithms are efficient for dense graphs and have applications in various fields such as network routing and database systems.

Uploaded by

megha200520
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

VISVESVARAYA TECHNOLOGICAL UNIVERSITY

“JnanaSangama”, Machhe, Belagavi, Karnataka-590018

2024 – 2025

Pedagogy Report
On

“Analysis and Design of Algorithms”

Submitted in partial fulfilment of the requirements for the award of the degree of

Bachelor of Engineering
in
Computer Science and Engineering
Submitted by

Archana K L (4VM23CS005) 4th Semester

Under the Supervision of


Prof. Vinutha N
Assistant Professor
Dept. of CSE, VVIET

DEPARTMENT OF COMPUTER SCIENCE


VIDYA VIKAS INSTITUTE OF ENGINEERING & TECHNOLOGY
#127-128, Mysore - Bannur Road, Alanahally, Mysuru, Karnataka 570028

1
TABLE OF CONTENTS:

SL.NO CONTENTS PAGE


NO

01 Introduction 3

02 Problem Definitions 3

03 Floyd’s Algorithm 4-6

9
04 Warshall’s Algorithm 7-9

05 Applications of Floyd’s and Warshall’s 9


Algorithms

06 Advantages and Limitations 10

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

Floyd’s Algorithm: All-Pairs Shortest Path:


Given a directed weighted graph with n vertices (and possible negative weights but no negative
cycles), compute the shortest path between every pair of vertices.

Warshall’s Algorithm: Transitive Closure


Given a directed graph represented by an adjacency matrix, compute its transitive closure — i.e.,
determine if a path exists between every pair of vertices using boolean logic.

Graph Representations in C++:

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

For a graph G(V, E) with n vertices:


1. Initialize a distance matrix dist with the input weights.
2. For each vertex k, update all distances dist[i][j] with:
3. dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
This checks if the path from i to j via k is shorter than the current direct path.

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;

#define INF INT_MAX


#define V 4

void floydWarshall(vector<vector<int>>& graph) {


vector<vector<int>> dist = graph;

for (int k = 0; k < V; ++k) {


for (int i = 0; i < V; ++i) {
for (int j = 0; j < V; ++j) {
if (dist[i][k] != INF && dist[k][j] != INF &&
dist[i][j] > dist[i][k] + dist[k][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}

cout << "Shortest distances between every pair of vertices:\n";


for (int i = 0; i < V; ++i) {
for (int j = 0; j < V; ++j) {
if (dist[i][j] == INF)
5
cout << "INF ";
else
cout << dist[i][j] << " ";
}
cout << endl;
}
}

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

void warshall(vector<vector<int>>& graph) {


vector<vector<int>> reach = graph;

for (int k = 0; k < V; ++k)


for (int i = 0; i < V; ++i)
for (int j = 0; j < V; ++j)
reach[i][j] = reach[i][j] || (reach[i][k] && reach[k][j]);

cout << "Transitive closure of the given graph:\n";


for (int i = 0; i < V; ++i) {
for (int j = 0; j < V; ++j) {
cout << reach[i][j] << " ";
}
cout << endl;
}
}

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

Applications of Floyd’s and Warshall’s Algorithms

• 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

Feature Floyd’s Algorithm Warshall’s Algorithm


Input Type Weighted graphs Boolean graphs
Detects negative cycles No No
Dynamic Programming Yes Yes
Complexity O(n³) O(n³)
Uses Distance calculation Reachability

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy