Assignment 04
Assignment 04
Assignment 04
Solution
The best approach to solve a problem like this would be spanning tree.
using System;
namespace dsa
{
class Program
{
static void Main(string[] args)
{
int verticesCount = 4;
int edgesCount = 5;
Graph graph = CreateGraph(verticesCount, edgesCount);
// Edge 0-1
graph.edge[0].Source = 0;
graph.edge[0].Destination = 1;
graph.edge[0].Weight = 10;
// Edge 0-2
graph.edge[1].Source = 0;
graph.edge[1].Destination = 2;
graph.edge[1].Weight = 6;
// Edge 0-3
graph.edge[2].Source = 0;
graph.edge[2].Destination = 3;
graph.edge[2].Weight = 5;
// Edge 1-3
graph.edge[3].Source = 1;
graph.edge[3].Destination = 3;
graph.edge[3].Weight = 15;
// Edge 2-3
graph.edge[4].Source = 2;
graph.edge[4].Destination = 3;
graph.edge[4].Weight = 4;
Kruskal(graph);
}
public struct Edge
{
public int Source;
public int Destination;
public int Weight;
}
public struct Graph
{
public int VerticesCount;
public int EdgesCount;
public Edge[] edge;
}
return graph;
}
return subsets[i].Parent;
}
if (x != y)
{
result[e++] = nextEdge;
Union(subsets, x, y);
}
}
Print(result, e);
}
}
}
Before After
10
0 1
0 10 1
6 5 15
5
2 3
2 3
4
4