mirror of
https://github.com/Lendaia/oe-alga-feladatok.git
synced 2026-04-23 12:26:07 +01:00
fortheloveofgod
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace OE.ALGA;
|
||||
|
||||
internal class FaElem<T> where T : IComparable
|
||||
public class FaElem<T> where T : IComparable
|
||||
{
|
||||
public T? tart;
|
||||
public FaElem<T>? bal;
|
||||
@@ -17,7 +17,7 @@ internal class FaElem<T> where T : IComparable
|
||||
}
|
||||
public class FaHalmaz<T> : Halmaz<T> where T : IComparable
|
||||
{
|
||||
FaElem<T>? gyoker;
|
||||
public FaElem<T>? gyoker;
|
||||
|
||||
private void BejarasPreOrder(FaElem<T> p, Action<T> muvelet)
|
||||
{
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace OE.ALGA.Adatszerkezetek
|
||||
{
|
||||
@@ -10,6 +13,7 @@ namespace OE.ALGA.Adatszerkezetek
|
||||
Suly = suly;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
public class CsucsmatrixSulyozottEgeszGraf : SulyozottGraf<int, SulyozottEgeszGrafEl>
|
||||
{
|
||||
@@ -26,7 +30,7 @@ namespace OE.ALGA.Adatszerkezetek
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
if (M[i,j] != null)
|
||||
if (M[i,j] != 0.0)
|
||||
{
|
||||
elek++;
|
||||
}
|
||||
@@ -62,7 +66,7 @@ namespace OE.ALGA.Adatszerkezetek
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
if (M[i,j] != null)
|
||||
if (M[i,j] != 0.0)
|
||||
{
|
||||
SulyozottEgeszGrafEl egeszgrafel = new SulyozottEgeszGrafEl(i, j, M[i,j]);
|
||||
fahalmaz.Beszur(egeszgrafel);
|
||||
@@ -75,7 +79,7 @@ namespace OE.ALGA.Adatszerkezetek
|
||||
|
||||
public float Suly(int honnan, int hova)
|
||||
{
|
||||
if(M[honnan, hova] != null)
|
||||
if(M[honnan, hova] != 0.0)
|
||||
{
|
||||
return M[honnan, hova];
|
||||
}
|
||||
@@ -90,7 +94,7 @@ namespace OE.ALGA.Adatszerkezetek
|
||||
FaHalmaz<int> fahalmaz = new FaHalmaz<int>();
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
if (M[csucs, i] != null)
|
||||
if (M[csucs, i] != 0.0)
|
||||
{
|
||||
fahalmaz.Beszur(i);
|
||||
}
|
||||
@@ -105,7 +109,109 @@ namespace OE.ALGA.Adatszerkezetek
|
||||
|
||||
public bool VezetEl(int honnan, int hova)
|
||||
{
|
||||
return (M[honnan, hova]!= null);
|
||||
return (M[honnan, hova]!= 0.0);
|
||||
}
|
||||
}
|
||||
public class Utkereses
|
||||
{
|
||||
public static Szotar<V, float> Dijkstra<V, E>(SulyozottGraf<V, E> g, V start) where V: IComparable
|
||||
{
|
||||
Szotar<V, float> legrovidebb = new HasitoSzotarTulcsordulasiTerulettel<V, float>(g.CsucsokSzama);
|
||||
Szotar<V, V> P = new HasitoSzotarTulcsordulasiTerulettel<V,V>(g.CsucsokSzama);
|
||||
KupacPrioritasosSor<V> S = new KupacPrioritasosSor<V>(g.CsucsokSzama, (ez, ennel) => legrovidebb.Kiolvas(ez) < legrovidebb.Kiolvas(ennel));
|
||||
|
||||
g.Csucsok.Bejar(x =>
|
||||
{
|
||||
legrovidebb.Beir(x, int.MaxValue);
|
||||
S.Sorba(x);
|
||||
});
|
||||
|
||||
legrovidebb.Beir(start, 0);
|
||||
S.Frissit(start);
|
||||
|
||||
while(!S.Ures)
|
||||
{
|
||||
V u = S.Sorbol();
|
||||
|
||||
g.Szomszedai(u).Bejar(x =>
|
||||
{
|
||||
if(legrovidebb.Kiolvas(u) + g.Suly(u,x) < legrovidebb.Kiolvas(x))
|
||||
{
|
||||
legrovidebb.Beir(x, legrovidebb.Kiolvas(u) + g.Suly(u,x));
|
||||
P.Beir(x, u);
|
||||
S.Frissit(x);
|
||||
}
|
||||
});
|
||||
}
|
||||
return legrovidebb;
|
||||
}
|
||||
}
|
||||
|
||||
public class FeszitofaKereses
|
||||
{
|
||||
public static Szotar<V, V> Prim<V, E>(SulyozottGraf<V, E> g, V start) where V: IComparable
|
||||
{
|
||||
Szotar<V, float> feszito = new HasitoSzotarTulcsordulasiTerulettel<V, float>(g.CsucsokSzama);
|
||||
Szotar<V, V> P = new HasitoSzotarTulcsordulasiTerulettel<V,V>(g.CsucsokSzama);
|
||||
KupacPrioritasosSor<V> S = new KupacPrioritasosSor<V>(g.CsucsokSzama, (ez, ennel) => feszito.Kiolvas(ez) < feszito.Kiolvas(ennel));
|
||||
FaHalmaz<V> helper = new FaHalmaz<V>();
|
||||
g.Csucsok.Bejar(x =>
|
||||
{
|
||||
feszito.Beir(x, int.MaxValue);
|
||||
S.Sorba(x);
|
||||
helper.Beszur(x);
|
||||
});
|
||||
feszito.Beir(start, 0);
|
||||
S.Frissit(start);
|
||||
|
||||
while(!S.Ures && helper.gyoker != null)
|
||||
{
|
||||
V minimum = S.Sorbol();
|
||||
helper.Torol(minimum);
|
||||
|
||||
g.Szomszedai(minimum).Bejar(x =>
|
||||
{
|
||||
if(helper.Eleme(x) && g.Suly(x, minimum) < feszito.Kiolvas(x))
|
||||
{
|
||||
feszito.Beir(x, g.Suly(x, minimum));
|
||||
P.Beir(x, minimum);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
return P;
|
||||
}
|
||||
|
||||
public static Halmaz<E> Kruskal<V, E>(SulyozottGraf<V, E> g) where E: SulyozottGrafEl<V>, IComparable
|
||||
{
|
||||
FaHalmaz<E> feszitofa = new FaHalmaz<E>();
|
||||
Szotar<V, int> vhalmaz = new HasitoSzotarTulcsordulasiTerulettel<V, int>(g.CsucsokSzama);
|
||||
int i = 0;
|
||||
g.Csucsok.Bejar(x => {vhalmaz.Beir(x, i++);});
|
||||
g.Elek.Bejar(e => {feszitofa.Beszur(e);});
|
||||
|
||||
while(feszitofa.gyoker != null)
|
||||
{
|
||||
|
||||
E minimum = feszitofa.gyoker.tart;
|
||||
feszitofa.Bejar(x =>
|
||||
{
|
||||
if(minimum.Suly > x.Suly)
|
||||
{
|
||||
minimum = x;
|
||||
}
|
||||
});
|
||||
|
||||
feszitofa.Torol(minimum);
|
||||
|
||||
if (vhalmaz.Kiolvas(minimum.Honnan) != vhalmaz.Kiolvas(minimum.Hova))
|
||||
{
|
||||
vhalmaz.Beir(minimum.Honnan, vhalmaz.Kiolvas(minimum.Hova));
|
||||
}
|
||||
|
||||
}
|
||||
return feszitofa;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user