/// <summary> /// Tworzy graf skierowany na podstawie spójnego grafu nieskierowanego /// </summary> /// <param name="matrix">Spójny graf nieskierowany</param> /// <returns>Graf skierowany</returns> public static DirectedGraphMatrix CreateDirectional(GraphMatrix matrix) { Random r = new Random(); DirectedGraphMatrix directedMatrix = new DirectedGraphMatrix(matrix.NodesNr); for (int i = 0; i < matrix.NodesNr; i++) { for (int j = 0; j <= i; j++) { if (matrix.GetConnection(i, j)) { switch (r.Next(3)) { case 0: directedMatrix.MakeConnection(i, j); break; case 1: directedMatrix.MakeConnection(j, i); break; case 2: directedMatrix.MakeConnection(i, j); directedMatrix.MakeConnection(j, i); break; } } } } return(directedMatrix); }
private void ConnectTwoNodes(int node1, int node2) { if (Graph.GetConnection(node1, node2) == false) { Graph.MakeConnection(node1, node2); Graph.OnChange(); } else { Graph.RemoveConnection(node1, node2); Graph.OnChange(); } }
public static DirectedGraphMatrix CreateRandomDirectedWeights(DirectedGraphMatrix f, int minWeight = -5, int maxWeight = 20) { Random r = new Random(); DirectedGraphMatrix ret = new DirectedGraphMatrix(f.NodesNr); for (int k = 0; k < f.NodesNr; k++) { for (int p = 0; p < f.NodesNr; p++) { if (f.GetConnection(k, p)) { ret.MakeConnection(k, p, r.Next(minWeight, maxWeight + 1)); } } } return(ret); }
/// SKIEROWANE KONWERSJE public static DirectedGraphMatrix ConvertToSMatrix(DirectedGraphMatrixInc from) { DirectedGraphMatrix x = new DirectedGraphMatrix(from.NodesNr); for (int i = 0; i < from.NodesNr; i++) { for (int j = 0; j < from.NodesNr; j++) { if (from.GetConnection(i, j)) { x.MakeConnection(i, j); } x.setWeight(i, j, from.getWeight(i, j)); } } return(x); }
private DirectedGraphMatrix createGraphFromRows(List <Row> rows) { DirectedGraphMatrix graph = new DirectedGraphMatrix(Node.Indexer); for (int i = 0; i < rows.Count; ++i) { var row = rows[i]; foreach (var node in row) { graph.AddToColumn(i); foreach (var connectedTo in node.JoinedTo) { graph.MakeConnection(node.Index, connectedTo.Index); } } } return(graph); }
private DirectedGraphMatrix createGraphFromRows(List <Row> rows) { DirectedGraphMatrix graph = new DirectedGraphMatrix(Node.Indexer); for (int i = 0; i < rows.Count; ++i) { var row = rows[i]; foreach (var node in row) { graph.AddToColumn(i); foreach (var connectedTo in node.JoinedTo) { var flow = flows.First(f => f.Item1.Index == node.Index && f.Item2.Index == connectedTo.Index); graph.MakeConnection(node.Index, connectedTo.Index); graph.setWeight(node.Index, connectedTo.Index, flow.Item3); } } } return(graph); }
/// <summary> /// Implementacja algorytmu Johnsona, korzysta z algorytmow Bellmana-Forda i Dijkstry /// </summary> /// <param name="g"></param> Musi to byc graf skierowany ktory ma juz randomowe wagi i musi byc on spojny(WAŻNE)!!!! /// Mozna np stworzyc graf skierowany, wydobyc z niego skladowa maksymalnie spojna i nadać jej randomowe wagi /// <returns></returns> Macierz odleglosci miedzy wszystkimi wierzcholkami public static int[,] Johnson(DirectedGraphMatrix graph) { try { int nodes = graph.NodesNr; int[,] distances = new int[nodes, nodes]; int[] d = new int[nodes]; const int INF = int.MaxValue - 10000; int[,] wagi = new int[nodes, nodes]; int q = nodes + 1; int[,] new_connect = new int[q, q]; for (int i = 0; i < nodes; ++i) { for (int j = 0; j < nodes; ++j) { new_connect[i, j] = graph.getConnect(i, j); wagi[i, j] = graph.getWeight(i, j); } } DirectedGraphMatrix dgraph = new DirectedGraphMatrix(q, new_connect); for (int i = 0; i < q - 1; ++i) { dgraph.MakeConnection(q - 1, i, 0); dgraph.setWeight(i, q - 1, INF); for (int j = 0; j < q - 1; ++j) { dgraph.setWeight(i, j, graph.getWeight(i, j)); } } List <List <int> > bellman = new List <List <int> >(); for (int i = 0; i < nodes; ++i) { bellman.Add(BellmanFord(dgraph, q - 1, i)); d[i] = pathWeight(dgraph, bellman[i]); if (ujemnyCykl(graph, wagi)) { throw new Exception("Algorytm Johnsona zostal zatrzymany."); } } for (int i = 0; i < q - 1; ++i) { for (int j = 0; j < q - 1; ++j) { if (dgraph.GetConnection(i, j)) { dgraph.setWeight(i, j, dgraph.getWeight(i, j) + d[i] - d[j]); } } } int[,] last_connect = new int[nodes, nodes]; for (int i = 0; i < nodes; ++i) { for (int j = 0; j < nodes; ++j) { last_connect[i, j] = dgraph.getConnect(i, j); } } DirectedGraphMatrix lgraph = new DirectedGraphMatrix(nodes, last_connect); for (int i = 0; i < nodes; ++i) { for (int j = 0; j < nodes; ++j) { lgraph.setWeight(i, j, dgraph.getWeight(i, j)); } } distances = distancesDirectedMatrix(lgraph); for (int i = 0; i < nodes; ++i) { for (int j = 0; j < nodes; ++j) { distances[i, j] = distances[i, j] - d[i] + d[j]; } } return(distances); } catch (Exception) { Console.WriteLine("Algorytm Johnsona zatrzymany z powodu ujemnego cyklu w grafie."); return(new int[5, 5]); } }