public void SmokeTest() { var edge1 = new Edge(1, 2, 1); var edge2 = new Edge(1, 3, 2); var edge3 = new Edge(1, 4, 3); var edge4 = new Edge(2, 3, 4); var edge5 = new Edge(2, 4, 5); var graph = new EdgeWeightedGraph(); graph.AddEdge(edge1); graph.AddEdge(edge2); graph.AddEdge(edge3); graph.AddEdge(edge4); graph.AddEdge(edge5); var adjacentTo1 = graph.Adjacent(1); Assert.AreEqual(3, adjacentTo1.Length); Assert.AreEqual(edge1, adjacentTo1[0]); Assert.AreEqual(edge2, adjacentTo1[1]); Assert.AreEqual(edge3, adjacentTo1[2]); var adjacentTo2 = graph.Adjacent(2); Assert.AreEqual(3, adjacentTo2.Length); Assert.AreEqual(edge1, adjacentTo2[0]); Assert.AreEqual(edge4, adjacentTo2[1]); Assert.AreEqual(edge5, adjacentTo2[2]); }
private EdgeWeightedGraph CreateGraph() { // Graph // 4 -[.8]- 2 // / / // [.2] [.3] // / / // 0 -[.5]- 1 // \ // [.6] // \ // 3 //----------------- // MST: // 4 2 // / / // [.2] [.3] // / / // 0 -[.5]- 1 // \ // [.6] // \ // 3 // Weight: 1.6 EdgeWeightedGraph graph = new EdgeWeightedGraph(5); graph.AddEdge(new Edge(0, 1, .5)); graph.AddEdge(new Edge(0, 4, .2)); graph.AddEdge(new Edge(1, 3, .6)); graph.AddEdge(new Edge(1, 2, .3)); graph.AddEdge(new Edge(4, 2, .8)); return(graph); }
public void Test_Counts_WhenAdding() { EdgeWeightedGraph graph = new EdgeWeightedGraph(3); graph.AddEdge(new Edge(0, 1, 0.5)); graph.AddEdge(new Edge(1, 2, 0.2)); graph.AddEdge(new Edge(2, 0, 0.2)); Assert.Equal(3, graph.EdgesCount); Assert.Equal(3, graph.VerticesCount); }
public void Test_GetEdges_Iteration() { EdgeWeightedGraph graph = new EdgeWeightedGraph(3); Edge e0 = new Edge(0, 1, .5); Edge e1 = new Edge(1, 2, 0.2); Edge e2 = new Edge(2, 0, 0.1); graph.AddEdge(e0); graph.AddEdge(e1); graph.AddEdge(e2); AssertUtilities.Sequence(new Edge[3] { e1, e0, e2 }, graph.GetEdges()); }
public void Test_Iteration() { EdgeWeightedGraph graph = new EdgeWeightedGraph(3); Edge e0 = new Edge(0, 1, 0.5); Edge e1 = new Edge(1, 2, 0.2); Edge e2 = new Edge(2, 0, 0.1); graph.AddEdge(e0); graph.AddEdge(e1); graph.AddEdge(e2); AssertUtilities.Sequence(new Edge[2] { e2, e0 }, graph.GetAdjacentVertices(0)); }
public IList <IList <int> > FindCriticalAndPseudoCriticalEdges(int n, int[][] edges) { var graph = new EdgeWeightedGraph <int, int>(n); for (var i = 0; i < edges.Length; i++) { graph.AddEdge(new WeightedEdge <int, int>(edges[i][0], edges[i][1], edges[i][2], i)); } var kruskal = new Kruskal <int, int>(graph); var mst = kruskal.MinimumSpanningTree; var weight = mst.Sum(edge => edge.Weight); var criticals = new HashSet <WeightedEdge <int, int> >(); foreach (var edge in mst) { graph.RemoveEdge(edge); try { if (new Kruskal <int, int>(graph).MinimumSpanningTree.Sum(edge => edge.Weight) > weight) { criticals.Add(edge); } } catch (ArgumentException) { criticals.Add(edge); } graph.AddEdge(edge); } var pseudos = new List <WeightedEdge <int, int> >(); foreach (var edge in graph.Edges) { if (!criticals.Contains(edge) && new Kruskal <int, int>(graph, edge).MinimumSpanningTree.Sum(edge => edge.Weight) == weight) { pseudos.Add(edge); } } var result0 = new List <int>(criticals.Count); var result1 = new List <int>(pseudos.Count); result0.AddRange(criticals.Select(edge => edge.Id)); result1.AddRange(pseudos.Select(edge => edge.Id)); return(new List <IList <int> >(2) { result0, result1 }); }
void MinTreeTest() { EdgeWeightedGraph g = new EdgeWeightedGraph(5); g.AddEdge(new Edge(0, 1, 1)); g.AddEdge(new Edge(1, 2, 2)); g.AddEdge(new Edge(1, 3, 6)); g.AddEdge(new Edge(2, 4, 4)); g.AddEdge(new Edge(3, 4, 5)); LazyPrimMST mst = new LazyPrimMST(g); //Debug.Log("延迟:" + mst.Weight()); PrimMST primMst = new PrimMST(g); //Debug.Log("即时:" + primMst.Weight()); KruskalMST kruskalMst = new KruskalMST(g); Debug.Log(kruskalMst.Weight()); }
public static EdgeWeightedGraph GenerateEdgeWeightedGraph(string file) { var lines = File.ReadAllLines(file); var vertices = int.Parse(lines[0]); var graph = new EdgeWeightedGraph(vertices); for (var i = 2; i < lines.Length; i++) { var str = lines[i].Split(' '); var v = int.Parse(str[0]); var w = int.Parse(str[1]); double weight = GetDoubleDot(str[2]); var e = new Edge(v, w, weight); graph.AddEdge(e); } return(graph); }
public static void Main(string[] args) { var filePath = @"D:\Github\Algorithms\AirlineNetworks\airlines\routes.txt"; var(routes, routeIds) = CsvParser.FileToRoutes(filePath); Console.WriteLine(routeIds.Count); var ewg = new EdgeWeightedGraph(routeIds.Count); foreach (var route in routes) { routeIds.TryGetValue(route.Source, out var source); routeIds.TryGetValue(route.Destination, out var destination); ewg.AddEdge(new Edge(source - 1, destination - 1, route.Distance)); } var adj = GetAdjacents("TUN"); foreach (var route in adj) { var source = GetAirportCode(route.V); var dest = GetAirportCode(route.W); Console.WriteLine($"{source};{dest} {route.Weight:N2}"); } var mst = new KruskalMST(ewg); Console.WriteLine(mst); var Edges = mst.Edges(); for (var i = 0; i < 200; i++) { Console.WriteLine(Edges.Dequeue()); } IEnumerable <Edge> GetAdjacents(string key) { routeIds.TryGetValue(key, out int val); return(ewg.Adjacents(val - 1)); } string GetAirportCode(int id) { return(routeIds.FirstOrDefault(x => x.Value == id + 1).Key); } }
// What's done immediately void Start() { GameObject starSpawn; Races.gen(minRaces, maxRaces); for (int i = 0; i < Races.size(); i++) // make all the homestars { starSpawn = createStarRandom(minStarSize, maxStarSize); Races.get(i).setHome(starSpawn); } //testShip (Races.size () - 1);// testing spawnShip if (starGridGeneration) { createStarsGrid(minStarSize / 2, maxStarSize / 2, Random.Range(minRandomStars, maxRandomStars)); } else { for (int i = 0; i < Random.Range(minRandomStars, maxRandomStars); i++) // spawn random stars { createStarRandom(minStarSize / 2, maxStarSize / 2); } } // STAR GRAPH THING fuelGraphWeightCost = 2.5f * refuelAmount; // 2.5 is roughly the amount of distance per fuel, multiplied by amount refueled at each star starGraph = new EdgeWeightedGraph(stars.Count); Edge tempEdge; float weight; for (int i = 0; i < stars.Count; i++) { for (int j = i + 1; j < stars.Count; j++) { weight = Vector3.Distance(stars[i].transform.position, stars[j].transform.position); weight -= fuelGraphWeightCost; // ACCOUNT FOR FUEL ------------------------------- tempEdge = new Edge(i, j, weight); starGraph.AddEdge(tempEdge); } } // find all parents. This is nessesary to do pathfind() from the star foreach (GameObject s in this.stars) { s.GetComponent <Star>().findParents(); } }
public void SmokeTest() { var edge1 = new Edge(0, 7, 0.16); var edge2 = new Edge(2, 3, 0.17); var edge3 = new Edge(1, 7, 0.19); var edge4 = new Edge(0, 2, 0.26); var edge5 = new Edge(5, 7, 0.28); var edge6 = new Edge(1, 3, 0.29); var edge7 = new Edge(1, 5, 0.32); var edge8 = new Edge(4, 5, 0.35); var edge9 = new Edge(1, 2, 0.36); var edge10 = new Edge(0, 4, 0.37); var edge11 = new Edge(0, 4, 0.38); var edge12 = new Edge(6, 2, 0.4); var edge13 = new Edge(3, 6, 0.52); var graph = new EdgeWeightedGraph(); graph.AddEdge(edge1); graph.AddEdge(edge2); graph.AddEdge(edge3); graph.AddEdge(edge4); graph.AddEdge(edge5); graph.AddEdge(edge6); graph.AddEdge(edge7); graph.AddEdge(edge8); graph.AddEdge(edge9); graph.AddEdge(edge10); graph.AddEdge(edge11); graph.AddEdge(edge12); graph.AddEdge(edge13); var mst = new LazyPrimMst(graph).Edges(); Assert.AreEqual(7, mst.Length); Assert.AreEqual(edge1, mst[0]); Assert.AreEqual(edge3, mst[1]); Assert.AreEqual(edge4, mst[2]); Assert.AreEqual(edge2, mst[3]); Assert.AreEqual(edge5, mst[4]); Assert.AreEqual(edge8, mst[5]); Assert.AreEqual(edge12, mst[6]); }
public EdgeWeightedGraph EdgeWeightGraphSample() { var ewg = new EdgeWeightedGraph(8); ewg.AddEdge(new Edge(4, 5, 0.35)); ewg.AddEdge(new Edge(4, 7, 0.37)); ewg.AddEdge(new Edge(5, 7, 0.28)); ewg.AddEdge(new Edge(0, 7, 0.16)); ewg.AddEdge(new Edge(1, 5, 0.32)); ewg.AddEdge(new Edge(0, 4, 0.38)); ewg.AddEdge(new Edge(2, 3, 0.17)); ewg.AddEdge(new Edge(1, 7, 0.19)); ewg.AddEdge(new Edge(0, 2, 0.26)); ewg.AddEdge(new Edge(1, 2, 0.36)); ewg.AddEdge(new Edge(1, 3, 0.29)); ewg.AddEdge(new Edge(2, 7, 0.34)); ewg.AddEdge(new Edge(6, 2, 0.40)); ewg.AddEdge(new Edge(3, 6, 0.52)); ewg.AddEdge(new Edge(6, 0, 0.58)); ewg.AddEdge(new Edge(6, 4, 0.93)); return(ewg); }
public void DrawSkelethMaze() { EdgeWeightedGraph MazeBasic = new EdgeWeightedGraph(25); // int tab[25]; Random rand; rand = new Random(); int xRand = rand.Next(); for(int i=0;i<5;i++) { for (int j = 0; j < 5; j++) { // for(int x=0;x<4;x++) int dest = ((i + 1) * 5) + j; if (i + 1 < 5) { xRand = (rand.Next() % 6) + 1; Edge tempEdge = new Edge(i * 5 + j, dest, xRand); MazeBasic.AddEdge(tempEdge); } dest = ((i - 1) * 5) + j; if (i - 1 >= 0) { xRand = (rand.Next() % 6) + 1; Edge tempEdge = new Edge(i * 5 + j, dest, xRand); MazeBasic.AddEdge(tempEdge); } dest = (i * 5) + (j + 1); if (j + 1 < 5) { xRand = (rand.Next() % 6) + 1; Edge tempEdge = new Edge(i * 5 + j, dest, xRand); MazeBasic.AddEdge(tempEdge); } dest = (i * 5) + (j - 1); if (j - 1 >= 0) { xRand = (rand.Next() % 6) + 1; Edge tempEdge = new Edge(i * 5 + j, dest, xRand); MazeBasic.AddEdge(tempEdge); } dest =( (i + 1) * 5) + (j + 1); if( j + 1 < 5 && i + 1 < 5) { xRand = (rand.Next() % 6) + 1; Edge tempEdge = new Edge(i * 5 + j, dest, xRand); MazeBasic.AddEdge(tempEdge); } } } PrimMST MazeBasicTree = new PrimMST(MazeBasic); // MazeBasicTree.Edges Queue<Edge> MazeDone = new Queue<Edge>(); int sorc,desto; String writelienString; foreach (Edge s in MazeBasicTree.Edges()) { // Debug.WriteLine( s.Target. +" "); sorc = s.Source(); desto = s.Dest(); writelienString = sorc.ToString() + " " + desto.ToString(); // Debug.WriteLine(writelienString); } }
public MazeStruct DrawMazeSkelethOfSize(int MazeSize,bool is_fivedirect) { EdgeWeightedGraph MazeBasic = new EdgeWeightedGraph(MazeSize * MazeSize); // int tab[25]; Random rand; rand = new Random(); int xRand = rand.Next(); for (int i = 0; i < MazeSize; i++) { for (int j = 0; j < MazeSize; j++) { // for(int x=0;x<4;x++) int dest = ((i + 1) * MazeSize) + j; if (i + 1 < MazeSize) { xRand = (rand.Next() % 6) + 1; Edge tempEdge = new Edge(i * MazeSize + j, dest, xRand); MazeBasic.AddEdge(tempEdge); } dest = ((i - 1) * MazeSize) + j; if (i - 1 >= 0) { xRand = (rand.Next() % 6) + 1; Edge tempEdge = new Edge(i * MazeSize + j, dest, xRand); MazeBasic.AddEdge(tempEdge); } dest = (i * MazeSize) + (j + 1); if (j + 1 < MazeSize) { xRand = (rand.Next() % 6) + 1; Edge tempEdge = new Edge(i * MazeSize + j, dest, xRand); MazeBasic.AddEdge(tempEdge); } dest = (i * MazeSize) + (j - 1); if (j - 1 >= 0) { xRand = (rand.Next() % 6) + 1; Edge tempEdge = new Edge(i * MazeSize + j, dest, xRand); MazeBasic.AddEdge(tempEdge); } if (is_fivedirect) { dest = ((i + 1) * MazeSize) + (j + 1); if (j + 1 < MazeSize && i + 1 < MazeSize) { xRand = (rand.Next() % 6) + 1; Edge tempEdge = new Edge(i * MazeSize + j, dest, xRand); MazeBasic.AddEdge(tempEdge); } } } } PrimMST MazeBasicTree = new PrimMST(MazeBasic); // MazeBasicTree.Edges Queue<Edge> MazeDone = new Queue<Edge>(); // Queue<Edge> MazeDone = new Queue<Edge>(); // int sorc, desto; // String writelienString; MazeStruct MazeToReturn; MazeToReturn = new MazeStruct(MazeSize* MazeSize); foreach (Edge s in MazeBasicTree.Edges()) { // Debug.WriteLine( s.Target. +" "); MazeToReturn.setUpByEdge(s,true); } return MazeToReturn; }
public void DrawMazeOfSize(int MazeSize) { EdgeWeightedGraph MazeBasic = new EdgeWeightedGraph(MazeSize*MazeSize); // int tab[25]; Random rand; rand = new Random(); int xRand = rand.Next(); for (int i = 0; i < MazeSize; i++) { for (int j = 0; j < MazeSize; j++) { // for(int x=0;x<4;x++) int dest = ((i + 1) * MazeSize) + j; if (i + 1 < MazeSize) { xRand = (rand.Next() % 6) + 1; Edge tempEdge = new Edge(i * MazeSize + j, dest, xRand); MazeBasic.AddEdge(tempEdge); } dest = ((i - 1) * MazeSize) + j; if (i - 1 >= 0) { xRand = (rand.Next() % 6) + 1; Edge tempEdge = new Edge(i * MazeSize + j, dest, xRand); MazeBasic.AddEdge(tempEdge); } dest = (i * MazeSize) + (j + 1); if (j + 1 < MazeSize) { xRand = (rand.Next() % 6) + 1; Edge tempEdge = new Edge(i * MazeSize + j, dest, xRand); MazeBasic.AddEdge(tempEdge); } dest = (i * MazeSize) + (j - 1); if (j - 1 >= 0) { xRand = (rand.Next() % 6) + 1; Edge tempEdge = new Edge(i * MazeSize + j, dest, xRand); MazeBasic.AddEdge(tempEdge); } dest = ((i + 1) * MazeSize) + (j + 1); if (j + 1 < MazeSize && i + 1 < MazeSize) { xRand = (rand.Next() % 6) + 1; Edge tempEdge = new Edge(i * MazeSize + j, dest, xRand); MazeBasic.AddEdge(tempEdge); } } } PrimMST MazeBasicTree = new PrimMST(MazeBasic); // MazeBasicTree.Edges Queue<Edge> MazeDone = new Queue<Edge>(); // MazeDone = MazeBasicTree.Edges(); //Edge TempEdge = new Edge(Center.Y,) // int sorc, desto; // String writelienString; // foreach (Edge s in MazeBasicTree.Edges()) // { // Debug.WriteLine( s.Target. +" "); // sorc = s.Source(); // desto = s.Dest(); // writelienString = sorc.ToString() + " " + desto.ToString(); // Debug.WriteLine(writelienString); // } }