/// <summary> /// Finds all the paths from currentServerNode to destServerNode as a recursive method. /// Passes all nodes, if destination node found, it is added to paths /// </summary> /// <param name="currentServerNode">Current server node</param> /// <param name="destServerNode">Destination server node</param> /// <param name="paths">All possible paths are inserted to this list</param> /// <param name="passedNodes"></param> private static void FindPaths(MDSServerNode currentServerNode, MDSServerNode destServerNode, ICollection <List <MDSServerNode> > paths, ICollection <MDSServerNode> passedNodes) { //Add current node to passedNodes to prevent multi-pass over same node passedNodes.Add(currentServerNode); //If current node is destination, then add passed nodes to found paths. if (currentServerNode == destServerNode) { var foundPath = new List <MDSServerNode>(); foundPath.AddRange(passedNodes); paths.Add(foundPath); } //Else, Jump to adjacents nodes of current node and conitnue searching else { foreach (var adjacentServerNode in currentServerNode.Adjacents.Values) { //If passed over this adjacentServerNode before, skip it if (passedNodes.Contains(adjacentServerNode)) { continue; } //Search path from this adjacent server to destination (recursive call) FindPaths(adjacentServerNode, destServerNode, paths, passedNodes); //Remove node from passed nodes, because we may pass over this node for searching another path passedNodes.Remove(adjacentServerNode); } } }
/// <summary> /// Find one of the shortest paths from given source node to destination node. /// </summary> /// <param name="sourceServerNode">Source node</param> /// <param name="destServerNode">Destination node</param> /// <returns>A path from source to destination</returns> private static List <MDSServerNode> FindShortestPath(MDSServerNode sourceServerNode, MDSServerNode destServerNode) { //Find all paths var allPaths = new List <List <MDSServerNode> >(); FindPaths(sourceServerNode, destServerNode, allPaths, new List <MDSServerNode>()); //Get shortest if (allPaths.Count > 0) { var bestPath = allPaths[0]; for (var i = 1; i < allPaths.Count; i++) { if (bestPath.Count > allPaths[i].Count) { bestPath = allPaths[i]; } } return(bestPath); } //No path from sourceServerNode to destServerNode return(null); }
/// <summary> /// Finds all the paths from currentServerNode to destServerNode as a recursive method. /// Passes all nodes, if destination node found, it is added to paths /// </summary> /// <param name="currentServerNode">Current server node</param> /// <param name="destServerNode">Destination server node</param> /// <param name="paths">All possible paths are inserted to this list</param> /// <param name="passedNodes"></param> private static void FindPaths(MDSServerNode currentServerNode, MDSServerNode destServerNode, ICollection<List<MDSServerNode>> paths, ICollection<MDSServerNode> passedNodes) { //Add current node to passedNodes to prevent multi-pass over same node passedNodes.Add(currentServerNode); //If current node is destination, then add passed nodes to found paths. if (currentServerNode == destServerNode) { var foundPath = new List<MDSServerNode>(); foundPath.AddRange(passedNodes); paths.Add(foundPath); } //Else, Jump to adjacents nodes of current node and conitnue searching else { foreach (var adjacentServerNode in currentServerNode.Adjacents.Values) { //If passed over this adjacentServerNode before, skip it if (passedNodes.Contains(adjacentServerNode)) { continue; } //Search path from this adjacent server to destination (recursive call) FindPaths(adjacentServerNode, destServerNode, paths, passedNodes); //Remove node from passed nodes, because we may pass over this node for searching another path passedNodes.Remove(adjacentServerNode); } } }
/// <summary> /// Find one of the shortest paths from given source node to destination node. /// </summary> /// <param name="sourceServerNode">Source node</param> /// <param name="destServerNode">Destination node</param> /// <returns>A path from source to destination</returns> private static List<MDSServerNode> FindShortestPath(MDSServerNode sourceServerNode, MDSServerNode destServerNode) { //Find all paths var allPaths = new List<List<MDSServerNode>>(); FindPaths(sourceServerNode, destServerNode, allPaths, new List<MDSServerNode>()); //Get shortest if (allPaths.Count > 0) { var bestPath = allPaths[0]; for (var i = 1; i < allPaths.Count; i++) { if (bestPath.Count > allPaths[i].Count) { bestPath = allPaths[i]; } } return bestPath; } //No path from sourceServerNode to destServerNode return null; }