-
Notifications
You must be signed in to change notification settings - Fork 1
/
CPrim.cs
executable file
·117 lines (99 loc) · 3.25 KB
/
CPrim.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace Editor_de_Gafos
{
public class CPrim
{
private CGrafo G;
private List<CNodoVertice> V;
private List<CArista> E;
private List<CVertice> U;
private TabPage tp;
public CPrim(CGrafo grafo,TabPage t)
{
V = grafo.getListaAdyacencia();
E = grafo.getListaAristas();
U = new List<CVertice>();
G = grafo;
tp = t;
}
public void prim()
{
Graphics g = Graphics.FromImage(G.getBMP());
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
List<CArista> T = new List<CArista>();
CArista uv = null;
U.Add(V[0].getVertice());
while (!UigualaV())
{
uv = buscaAristaCostoMin();
T.Add(uv);
U.Add(uv.getVDestino());
}
T.Sort(comparaAristas);
string cad = " Conjunto de Aristas\n\n T : {";
foreach (CArista a in T)
{
a.dibujateAACM(g, G.getBMP(), tp);
cad += " (" + a.getVOrigen().getId().ToString() +","+a.getVDestino().getId().ToString()+") ";
}
cad+="}. ";
MessageBox.Show(cad,"Árbol Abarcador de Costo Mínimo (Algoritmo de PRIM)",MessageBoxButtons.OK,MessageBoxIcon.Information);
g.Clear(Color.White);
G.dibujate(tp, G.getBMP());
}
public bool UigualaV()
{
bool igual = true;
if (U.Count == V.Count)
{
foreach (CNodoVertice cnv in V)
{
if (!U.Contains(cnv.getVertice()))
igual = false;
}
}
else
igual = false;
return igual;
}
public List<CVertice> VmenosU()
{
List<CVertice> vmu = new List<CVertice>();
foreach (CNodoVertice cnv in V)
{
if (!U.Contains(cnv.getVertice()))
vmu.Add(cnv.getVertice());
}
return vmu;
}
public CArista buscaAristaCostoMin()
{
List<CArista> posibles = new List<CArista>();
List<CVertice> umv = VmenosU();
foreach (CArista ar in E)
{
if ((U.Contains(ar.getVOrigen()) && umv.Contains(ar.getVDestino())) /*||
(U.Contains(ar.getVDestino()) && umv.Contains(ar.getVOrigen()))*/)
{
posibles.Add(ar);
}
}
posibles.Sort(comparaAristas);
CArista amc = posibles[0];
foreach (CArista ar in posibles)
{
if (ar.getPeso() < amc.getPeso())
amc = ar;
}
return amc;
}
public int comparaAristas(CArista a1, CArista a2)
{
return a1.getPeso().CompareTo(a2.getPeso());
}
}
}