Skip to content
Snippets Groups Projects
Commit e19635f6 authored by Gianluca's avatar Gianluca
Browse files

da finire Lab

parent 429f378a
No related branches found
No related tags found
No related merge requests found
import it.uniupo.graphLib.UndirectedGraph;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class TestPrim {
@Test
void testNullCostructor() {
UndirectedGraph g = new UndirectedGraph("4;1 2 2;1 0 5;2 3 3; 0 3 1");
Prim prim = new Prim(g);
Assertions.assertNotNull(prim);
}
@Test
void testGetMST() {
UndirectedGraph g = new UndirectedGraph("4;1 2 2;1 0 5;2 3 3; 0 3 1");
Prim prim = new Prim(g);
UndirectedGraph MST = prim.getMST();
Assertions.assertTrue(MST.hasEdge(0, 3));
Assertions.assertTrue(MST.hasEdge(3, 2));
Assertions.assertTrue(MST.hasEdge(2, 1));
Assertions.assertFalse(MST.hasEdge(0, 1));
}
@Test
void testGetCost() {
UndirectedGraph g = new UndirectedGraph("4;1 2 2;1 0 5;2 3 3; 0 3 1");
Prim prim = new Prim(g);
Assertions.assertEquals(6, prim.getCost());
}
}
import it.uniupo.algoTools.MST;
import it.uniupo.algoTools.MinHeap;
import it.uniupo.algoTools.UnionByRank;
import it.uniupo.algoTools.UnionFind;
import it.uniupo.graphLib.Edge;
import it.uniupo.graphLib.UndirectedGraph;
public class Kruskal implements MST {
private final UndirectedGraph myGraph;
public Kruskal(UndirectedGraph g) {
myGraph = g;
}
private int kruskal(UndirectedGraph MST) {
UndirectedGraph myGraphCopy = myGraph;
MinHeap<Edge, Integer> minHeap = new MinHeap<>();
int cost = 0;
for (int i = 0; i < myGraph.getOrder(); ++i) {
for (Edge e : myGraphCopy.getOutEdges(i)) {
minHeap.add(e, e.getWeight());
myGraphCopy.removeEdge(e.getHead(), e.getTail());
}
}
UnionFind unionFind = new UnionByRank(myGraph.getOrder());
while (!minHeap.isEmpty()) {
Edge e = minHeap.extractMin();
int tail = e.getTail();
int head = e.getHead();
int tailLeader = unionFind.find(tail);
int headLeader = unionFind.find(head);
if (tailLeader != headLeader) {
MST.addEdge(e);
cost += e.getWeight();
unionFind.union(tailLeader, headLeader);
}
}
return cost;
}
@Override
public MST create(UndirectedGraph undirectedGraph) {
return new Kruskal(myGraph);
}
@Override
public UndirectedGraph getMST() {
UndirectedGraph MST = (UndirectedGraph) myGraph.create();
kruskal(MST);
return MST;
}
@Override
public int getCost() {
UndirectedGraph MST = (UndirectedGraph) myGraph.create();
return kruskal(MST);
}
}
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
\ No newline at end of file
import it.uniupo.algoTools.MST;
import it.uniupo.algoTools.MinHeap;
import it.uniupo.graphLib.Edge;
import it.uniupo.graphLib.UndirectedGraph;
public class Prim implements MST {
private final UndirectedGraph myGraph;
private boolean[] founded;
public Prim(UndirectedGraph g) {
myGraph = g;
founded = new boolean[myGraph.getOrder()];
}
private int prim(int sorg, UndirectedGraph MST) {
founded[sorg] = true;
MinHeap<Edge, Integer> minHeap = new MinHeap<>();
int cost = 0;
for (Edge e : myGraph.getOutEdges(sorg)) {
minHeap.add(e, e.getWeight());
}
while (!minHeap.isEmpty()) {
Edge e = minHeap.extractMin();
int v = e.getHead();
if (!founded[v]) {
founded[v] = true;
MST.addEdge(e);
cost += e.getWeight();
for (Edge w : myGraph.getOutEdges(v)) {
minHeap.add(w, w.getWeight());
}
}
}
return cost;
}
@Override
public MST create(UndirectedGraph undirectedGraph) {
return new Prim(myGraph);
}
@Override
public UndirectedGraph getMST() {
UndirectedGraph MST = (UndirectedGraph) myGraph.create();
prim(0, MST);
return MST;
}
@Override
public int getCost() {
UndirectedGraph MST = (UndirectedGraph) myGraph.create();
return prim(0, MST);
}
}
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment