23350 Assignment Graphs 02
23350 Assignment Graphs 02
DSA (ITC303)
AY: 2024-25
SUBMITTED BY
ROHIT KUMAR(23350)
III SEMESTER
BACHELOR OF TECHNOLOGY
in
INFORMATION TECHNOLOGY
23350
ASSIGNMENT- 10 (Graphs2)
Coding:-
#include <iostream>
#include <vector>
#include <algorithm>
// Kruskal's algorithm
void kruskalMST(Graph& graph) {
int V = graph.V;
vector<Edge> result; // To store the resulting MST
int e = 0; // Number of edges in MST
int main() {
int V = 4; // Number of vertices
int E = 5; // Number of edges
Graph graph;
graph.V = V;
graph.E = E;
{0, 3, 5},
{1, 3, 15},
{2, 3, 4}
};
kruskalMST(graph);
return 0;
}
#include <iostream>
#include <climits>
// Prim's Algorithm
void primMST(int graph[][5], int V) {
int parent[5]; // Array to store the MST
23350
int main() {
int V = 5; // Number of vertices
int graph[5][5] = {
{0, 2, 0, 6, 0},
{2, 0, 3, 8, 5},
{0, 3, 0, 0, 7},
{6, 8, 0, 0, 9},
{0, 5, 7, 9, 0}
};
primMST(graph, V);
return 0;
}
23350
3. WAP to find the shortest path from a source node to all other nodes in a
weighted graph:-
#include <iostream>
#include <climits>
cout << "Vertex\tDistance from Source (" << src << ")\n";
for (int i = 0; i < V; i++) {
cout << i << "\t" << dist[i] << "\n";
}
}
int main() {
int V = 5; // Number of vertices
int graph[5][5] = {
{0, 10, 0, 0, 5},
{0, 0, 1, 0, 2},
{0, 0, 0, 4, 0},
{7, 0, 6, 0, 0},
{0, 3, 9, 2, 0}
};
return 0;
}
4. WAP to find the shortest path from a source node to all other nodes in a weighted
graph using Dijkstra's algorithm:-
#include <iostream>
#include <climits>
int main() {
int V = 6; // Number of vertices
int graph[6][6] = {
{0, 4, 0, 0, 0, 0},
{4, 0, 8, 0, 0, 0},
{0, 8, 0, 7, 0, 4},
{0, 0, 7, 0, 9, 14},
{0, 0, 0, 9, 0, 10},
{0, 0, 4, 14, 10, 0}
};
return 0;
}
23350
5. WAP to find the shortest path from a source node to all other nodes in a
weighted graph using Floyd- Warshall Algorithm:-
#include <iostream>
#include <climits>
// Floyd-Warshall Algorithm
void floydWarshall(int graph[][4], int V) {
int dist[4][4];
int main() {
int V = 4; // Number of vertices
int graph[4][4] = {
{0, 3, INF, 7},
{8, 0, 2, INF},
{5, INF, 0, 1},
{2, INF, INF, 0}
};
floydWarshall(graph, V);
return 0;
}
➢ Code:-
#include <iostream>
#include <queue>
#include <vector>
if (inDegree[i] == 0) {
q.push(i);
}
}
int main() {
int V = 6; // Number of vertices
vector<int> adj[V]; // Adjacency list representation
topologicalSort(V, adj);
return 0;
}
23350
i) Task Scheduling:-
• Dependency resolution between tasks (e.g., job scheduling, build
systems like Make).
• Example: Build order of software components with dependencies.
ii) Course Scheduling:-
• Determining the order in which courses should be taken given
prerequisites.
iii) Critical Path Method (CPM):-
• In project management, to find the sequence of tasks that determines
the project duration.
iv) Dependency Management:-
• Package management systems (e.g., resolving installation order of
libraries or modules).
v) Compiler Design:-
• Order of compilation for modules with dependencies.
vi) Data Serialization:-
• Determining the order in which objects need to be saved or loaded
while preserving dependencies.
7. Try: WAP to check if a graph is bipartite, and what does it mean for a graph to
be bipartite in terms of graph coloring:-
#include <iostream>
#include <vector>
#include <queue>
q.push(start);
color[start] = 0; // Start coloring with 0
while (!q.empty()) {
int u = q.front();
q.pop();
int main() {
int V = 6; // Number of vertices
vector<int> adj[V]; // Adjacency list representation
return 0;
}
23350
i) 2-Colorable: A bipartite graph can be colored using two colors such that
no two adjacent vertices share the same color.
ii) No Odd Cycles: A graph is bipartite if and only if it does not contain any
odd-length cycles.
iii) Vertex Partition: The vertices can be partitioned into two disjoint sets
such that every edge connects vertices from different sets.
8. Try: Given a grid where 1 represents open cells and 0 represents walls, find the
shortest path from a start cell to an end cell:-
Hint: Use BFS for shortest paths in unweighted grids and practice navigating 2D
grids.
#include <iostream>
#include <queue>
#include <vector>
// Function to check if a cell is valid (inside the grid and not a wall)
bool isValid(int x, int y, int rows, int cols, vector<vector<int>>& grid,
vector<vector<bool>>& visited) {
return x >= 0 && x < rows && y >= 0 && y < cols && grid[x][y] == 1 && !visited[x][y];
}
while (!q.empty()) {
auto current = q.front();
q.pop();
int x = current.first.first;
int y = current.first.second;
int dist = current.second;
23350
// If no path exists
return -1;
}
int main() {
// Example grid
vector<vector<int>> grid = {
{1, 1, 1, 0, 0},
{1, 0, 1, 0, 1},
{1, 0, 1, 1, 1},
{0, 0, 0, 0, 1},
{1, 1, 1, 1, 1}
};
// Output result
if (result != -1) {
cout << "The shortest path length is: " << result << endl;
} else {
cout << "No path exists from start to end." << endl;
}
return 0;