You are on page 1of 6

BÀI 1: TÌM CÂY KHUNG

public class ConvertGraph


{
static int n, u, v, w;
static bool[] visited;
public static List<Tuple<int,int>>[] adjList;
static LinkedList<Tuple<int, int, int>> tree = new LinkedList<Tuple<int, int,int>>();

static void ReadEdgeList(string path, out int n, out int m, out List<Tuple<int, int,int>> edgeList)
{
StreamReader sr = new StreamReader(path);
string line = sr.ReadLine();
string[] values = line.Split();
n = Convert.ToInt32(values[0]);
m = Convert.ToInt32(values[1]);
edgeList = new List<Tuple<int, int, int>>();

for (int i = 0; i < m; i++)


{
line = sr.ReadLine();
values = line.Split();
u = Convert.ToInt32(values[0]);
v = Convert.ToInt32(values[1]);
w = Convert.ToInt32(values[2]);
Tuple<int, int,int> e = new Tuple<int, int,int>(u, v,w);
edgeList.Add(e);
}
sr.Close();
}

static void ConvertEdgeList2AdjList(int n, int m, List<Tuple<int, int, int>> edgeList, out List<Tuple<int, int>>[]
adjList)
{
adjList = new List<Tuple<int, int>>[n];

for (int i = 0; i < n; i++)


{
adjList[i] = new List<Tuple<int, int>>();
}

foreach (var edge in edgeList)


{
int u = edge.Item1;
int v = edge.Item2;
int w = edge.Item3;
adjList[u - 1].Add(new Tuple<int, int>(v, w));
adjList[v - 1].Add(new Tuple<int, int>(u, w));
}
}

static void Main(string[] args)


{
List<Tuple<int, int, int>> edgeList;
ReadEdgeList("C:/Bai1.txt", out n, out int m, out edgeList);
ConvertEdgeList2AdjList(n, m, edgeList, out adjList);
Console.WriteLine("So dinh la: " + n);
for (int i = 0; i < n; i++)
{
Console.Write(i + 1 + " | ");
foreach (var v in adjList[i])
{
Console.Write(v + " ");
}
Console.WriteLine();
}

Initialize();
SpanningTree(0);
}
public static void SpanningTree(int s)
{
visited[s] = true;
TreeDFS(s);
PrintSpanningTree();
}
static void Initialize()
{
visited = new bool[n];
for (int i = 0; i < visited.Length; i++)
{
visited[i] = false;
}
}

private static void TreeDFS(int s)


{
foreach (var a in adjList[s])
{
int u = a.Item1;
int w = a.Item2;
if (!visited[u-1])
{
visited[u-1] = true;
tree.AddLast(new Tuple<int, int, int>(s, u, w));
TreeDFS(u-1);
}
}
}
private static void PrintSpanningTree()
{
Console.WriteLine("Cay khung la:");
Console.WriteLine(tree.Count);
foreach (var edge in tree)
{
Console.WriteLine(edge.Item1+1 + " " + edge.Item2+" "+edge.Item3);
}
}
}

BÀI 2: CÂY KHUNG NHỎ NHẤT THEO THUẬT


TOÁN KRUSKAL
using System;
using System.Collections.Generic;

public class Graph


{
private List<Tuple<int, int, int>> g;
private int n;
private int m;
private List<Tuple<int, int, int>> tree;
private bool[,] connected;
private bool[] label;

public Graph(int vertexCount)


{
n = vertexCount;
g = new List<Tuple<int, int, int>>();
connected = new bool[n + 1, n + 1];
label = new bool[n + 1];
tree = new List<Tuple<int, int, int>>();
}

public void AddEdge(int v, int u, int w)


{
g.Add(new Tuple<int, int, int>(v, u, w));
}

public void Kruskal()


{
g.Sort((x, y) => x.Item3.CompareTo(y.Item3));

for (int i = 1; i <= n; i++)


{
for (int j = 1; j <= n; j++)
{
connected[i, j] = false;
}
}

foreach (var edge in g)


{
if (tree.Count == n - 1)
{
break;
}

int v = edge.Item1;
int u = edge.Item2;

if (!IsConnected(v, u))
{
tree.Add(edge);
Union(v, u);
}
}

ShowTree();
}

private bool IsConnected(int u, int v)


{
return connected[u, v];
}

private void Union(int u, int v)


{
connected[u, v] = true;
connected[v, u] = true;
}

public void ShowTree()


{
int totalw = 0;
foreach (var edge in tree)
{
totalw += edge.Item3;
}
Console.WriteLine(n-1+" "+totalw);
foreach (var edge in tree)
{
Console.WriteLine($"{edge.Item1} {edge.Item2} {edge.Item3}");
}
}
}

public class Program


{
public static void Main(string[] args)
{
Graph graph = new Graph(4);
graph.AddEdge(1, 2, 5);
graph.AddEdge(1, 3, 4);
graph.AddEdge(1, 4, 2);
graph.AddEdge(2, 3, 7);
graph.AddEdge(3, 4, 8);
graph.Kruskal();
}
}

BÀI 3: PRIM
using System;
using System.Collections.Generic;

public class Graph


{
private List<Tuple<int, int, int>> g;
private int n;
private List<Tuple<int, int, int>> tree;
private bool[] label;

public Graph(int v)
{
n = v;
g = new List<Tuple<int, int, int>>();
label = new bool[n + 1];
tree = new List<Tuple<int, int, int>>();
}

public void AddEdge(int v, int u, int w)


{
g.Add(new Tuple<int, int, int>(v, u, w));
}

public void Prim(int u)


{
label[u] = true;
Tuple<int, int, int> e = new Tuple<int, int, int>(0, 0, 0);

while (tree.Count < n - 1)


{
e = dmin();
tree.Add(e);
label[e.Item1] = true;
label[e.Item2] = true;
}

ShowTree();
}

public Tuple<int, int, int> dmin()


{
Tuple<int, int, int> emin = new Tuple<int, int, int>(int.MaxValue, int.MaxValue, int.MaxValue);

foreach (var edge in g)


{
int u = edge.Item1;
int v = edge.Item2;

if (label[u] && !label[v] && edge.Item3 < emin.Item3)


{
emin = edge;
}
else if (!label[u] && label[v] && edge.Item3 < emin.Item3)
{
emin = edge;
}
}

return emin;
}

public void ShowTree()


{
int totalWeight = 0;
foreach (var edge in tree)
{
totalWeight += edge.Item3;
}
Console.WriteLine(n-1+" "+totalWeight);
foreach (var edge in tree)
{
Console.WriteLine($"{edge.Item1} {edge.Item2} {edge.Item3}");
}
}
}

public class Program


{
public static void Main(string[] args)
{
Graph graph = new Graph(4);
graph.AddEdge(1, 2, 5);
graph.AddEdge(1, 3, 4);
graph.AddEdge(1, 4, 2);
graph.AddEdge(2, 3, 7);
graph.AddEdge(3, 4, 8);
graph.Prim(1);
}
}

You might also like