public static uint[] GetPath(DijkstraAlgoData dad) { var count_walker = dad.prev_node[dad.ending_node]; int count = 0; while (count_walker != INVALID_NODE) { count += 1; count_walker = dad.prev_node[count_walker]; } if (count == 0) { return(null); } else { var ret = new uint[count]; int i = 1; var path_walker = dad.ending_node; while (path_walker != dad.starting_node) // i <= count { ret[count - i] = path_walker; i += 1; path_walker = dad.prev_node[path_walker]; } return(ret); } }
public static DijkstraAlgoData StartDijkstra(DijkstraGraph graph, uint starting_node, uint ending_node) { var dad = new DijkstraAlgoData(); dad.graph = graph; dad.starting_node = starting_node; dad.ending_node = ending_node; dad.node_cost = new uint[graph.nodes.Length]; dad.prev_node = new uint[graph.nodes.Length]; for (int i = 0; i < graph.nodes.Length; ++i) { dad.node_cost[i] = 1000000000; dad.prev_node[i] = INVALID_NODE; } dad.node_cost[starting_node] = 0; dad.open_list = new uint[graph.nodes.Length]; dad.open_list[0] = starting_node; dad.open_list_size = 1; return(dad); }
public static bool Process(DijkstraAlgoData dad) { var node = dad.open_list[dad.open_list_size - 1]; dad.open_list_size -= 1; for (int i = 0; i < dad.graph.edges.Length; i += 3) { var edge_start = dad.graph.edges[i]; var edge_end = dad.graph.edges[i + 1]; var cost = dad.graph.edges[i + 2]; if (edge_end == node) { var v = edge_start; edge_start = edge_end; edge_end = v; } if (edge_start == node) { if (dad.node_cost[edge_end] > (dad.node_cost[node] + cost)) { dad.node_cost[edge_end] = (dad.node_cost[node] + cost); dad.prev_node[edge_end] = node; dad.open_list[dad.open_list_size] = edge_end; dad.open_list_size += 1; } } } if (dad.open_list_size == 0) { return(true); } else { return(false); } }