0% found this document useful (0 votes)
30 views

Assignment 04

The document describes a problem of finding minimum risk paths to transmit a message between computers in a wired network. It proposes using Kruskal's algorithm to find a minimum spanning tree over the graph of wires between computers, where each wire has an associated risk factor. It provides code to implement Kruskal's algorithm on a sample graph with 4 computers and 5 wires between them. The code finds the minimum risk spanning tree and prints the selected wires and their total risk factor.

Uploaded by

Arsalan Jah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Assignment 04

The document describes a problem of finding minimum risk paths to transmit a message between computers in a wired network. It proposes using Kruskal's algorithm to find a minimum spanning tree over the graph of wires between computers, where each wire has an associated risk factor. It provides code to implement Kruskal's algorithm on a sample graph with 4 computers and 5 wires between them. The code finds the minimum risk spanning tree and prints the selected wires and their total risk factor.

Uploaded by

Arsalan Jah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

DATA STRUCTURES AND ALGORITHMS

Assignment 04

Syed Muhammad Arsalan Jah (02-131212-078)


Muhammad Zain (02-131212-077)
Hamza Zafar (02-131212-079)
Task
Suppose a company has a wired network within its premises. Users can easy share files from their system to every other
system as fast as possible. Unfortunately, the security measures of the company are insufficient. Wires can be monitored by
shadowy organization who can intercepts your messages.
After doing some preliminary research, you are able to assign each wire a “risk factor” indicating the likelihood that wire is
being monitored. For example, if a wire has a risk factor of zero, it is extremely unlikely to be monitored; if a wire has a risk
factor of 10, it is more likely to be monitored. The smallest possible risk factor is 0; the largest possible risk factor is n.
Design and implement an efficient data structure algorithm that selects wires to send your message such that (a) every
computer receives the message and (b) you minimize the total risk factor. The total risk factor is defined as the sum of the
risks of all the wires you use.

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;
}

public struct Subset


{
public int Parent;
public int Rank;
}

public static Graph CreateGraph(int verticesCount, int edgesCoun)


{
Graph graph = new Graph();
graph.VerticesCount = verticesCount;
graph.EdgesCount = edgesCoun;
graph.edge = new Edge[graph.EdgesCount];

return graph;
}

private static int Find(Subset[] subsets, int i)


{
if (subsets[i].Parent != i)
subsets[i].Parent = Find(subsets, subsets[i].Parent);

return subsets[i].Parent;
}

private static void Union(Subset[] subsets, int x, int y)


{
int xroot = Find(subsets, x);
int yroot = Find(subsets, y);

if (subsets[xroot].Rank < subsets[yroot].Rank)


subsets[xroot].Parent = yroot;
else if (subsets[xroot].Rank > subsets[yroot].Rank)
subsets[yroot].Parent = xroot;
else
{
subsets[yroot].Parent = xroot;
++subsets[xroot].Rank;
}
}

private static void Print(Edge[] result, int e)


{
int sum = 0;
Console.WriteLine("The network with minimum risk factors is shown below: \n");
for (int i = 0; i < e; ++i)
{
Console.WriteLine("Computer {0} -- Computer {1} \tRisk Facter: {2}",
result[i].Source, result[i].Destination, result[i].Weight);
sum += result[i].Weight;
}
Console.WriteLine("\nSUM OF ALL RISK FACTORS: {0}",sum);
}

public static void Kruskal(Graph graph)


{
int verticesCount = graph.VerticesCount;
Edge[] result = new Edge[verticesCount];
int i = 0;
int e = 0;

Array.Sort(graph.edge, delegate (Edge a, Edge b)


{
return a.Weight.CompareTo(b.Weight);
});

Subset[] subsets = new Subset[verticesCount];

for (int v = 0; v < verticesCount; ++v)


{
subsets[v].Parent = v;
subsets[v].Rank = 0;
}

while (e < verticesCount - 1)


{
Edge nextEdge = graph.edge[i++];
int x = Find(subsets, nextEdge.Source);
int y = Find(subsets, nextEdge.Destination);

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

You might also like