// function gorr djikstras algo public void djikstra(long[,] graph, Node source, Node end) { lastEdge = new Undirected_Edge(source.location, end.location); bool[] visited = new bool[no_of_nodes]; Node[] previousnode = new Node[no_of_nodes]; foreach (Node n in Nodes) { // initialise shortestdistance.Add(n.location, graph[source.location, n.location]); visited[n.location] = false; previousnode[n.location] = source; } visited[source.location] = true; //order by value shortestdistance.OrderBy(pair => pair.Value).ToDictionary(pair => pair.Key, pair => pair.Value); foreach (Node no in Nodes) { // get the minimum vaalue distance not visited for (int v = 0; v < no_of_nodes; ++v) { if (visited[v] == false) { keynode = Nodes[shortestdistance.ElementAt(v).Key]; keyvalue = shortestdistance[keynode.location]; } } visited[keynode.location] = true; if (keynode.location == end.location) { //Console.WriteLine("distance fronm " + source.location + " to " + end.location); //Console.WriteLine(shortestdistance[keynode.location]); shortestpath sm = new shortestpath(source, end); sm.weight = shortestdistance[keynode.location]; lastEdge.weight = sm.weight; Node i = end; while (i.location != source.location) { Node currentnode = i; i = previousnode[currentnode.location]; Undirected_Edge e = new Undirected_Edge(currentnode.location, i.location); sm.pathEdges.Add(e); //Console.WriteLine(currentnode.location + " to " + i.location); } lastEdge.arbitarypath = sm; lastEdge.id += "arbitary"; minimumHamiltonCycle.edges.Add(lastEdge.id, lastEdge); } foreach (Node n in Nodes) { long newdistance = keyvalue + graph[keynode.location, n.location]; long olddistance = shortestdistance[n.location]; if (newdistance < olddistance) { shortestdistance[n.location] = newdistance; previousnode[n.location] = keynode; } } shortestdistance.OrderBy(pair => pair.Value).ToDictionary(pair => pair.Key, pair => pair.Value); } }
public void printArbitaryPath(Undirected_Edge ed) { Console.WriteLine("Arbitary path for " + ed.id); foreach (Undirected_Edge e in ed.arbitarypath.pathEdges) { Console.WriteLine(e.id); Console.WriteLine(e.weight); } }
// function to finde edged till last max value edge for whic we can yse a shortest path for arbitary path public void FindMinHamiltoncycle() { foreach (Node n in Nodes) { //Console.WriteLine("check node " + n.location); //Console.WriteLine("possible edges" + n.possible_edges); //Console.WriteLine("edges found " + n.edges_found); if (n.edges_found == 2) { //if both edges are found it wont do anything } else { while (n.index <= n.possible_edges) { long check = n.distance.ElementAt(n.index).Key; //nodes[check ]is the other node to check for (int index2 = 1; index2 <= Nodes[check].possible_edges; index2++)// min range 2 only increase if necessary { long check2 = Nodes[check].distance.ElementAt(index2).Key; //Console.WriteLine("node :" + n.location + " at check : " + n.index + " goes to " + check + " node: " + check + " atcheck :" + index2 + " goes to " + check2); bool notloop = true; // prevents loop and keeps the cycle one edge missing if (n.adjacentNodes.Count != 0) { if (n.adjacentNodes.ElementAt(0).adjacentNodes != null) { //Console.WriteLine("adjacent node" + n.adjacentNodes.ElementAt(0).location); //Console.WriteLine("adjacent nof of " + n.adjacentNodes.ElementAt(0).adjacentNodes.ElementAt(0).location); foreach (Node m in n.adjacentNodes.ElementAt(0).adjacentNodes) { if (m.location == check) { notloop = false; } } } } if (n.location == Nodes[check].distance.ElementAt(index2).Key&& Nodes[check].edges_found != 2 && notloop) { //Console.WriteLine("node1:" + check + " node2:" + check2 + "match"); n.index++; Undirected_Edge temp = new Undirected_Edge(check, check2); Undirected_Edge e; //Console.WriteLine("id:" + ed.id); // below code prevents duplicates if (minimumHamiltonCycle.edges.Count == no_of_nodes - 1) // get up to only -1 edges are left { return; } if (minimumHamiltonCycle.edges.TryGetValue(temp.id, out e)) { } else { minimumHamiltonCycle.edges.Add(temp.id, temp); n.edges_found += 1; Nodes[check].edges_found += 1; //Console.WriteLine(n.edges_found + " edges found for node " + n.location + " and node " + Nodes[check].location); n.adjacentNodes.Add(Nodes[check]); Nodes[check].adjacentNodes.Add(n); } } } n.index++; } } } foreach (Node n in Nodes) { if (n.edges_found != 2) { n.possible_edges += 1; } } if (minimumHamiltonCycle.edges.Count < no_of_nodes - 1) { FindMinHamiltoncycle(); } }