public AdjacencyList Clone() { AdjacencyList list = new AdjacencyList(); list.Start = Start; list.End = End; foreach (Node node in List) { list.List.Add(node); } return(list); }
private static List <List <Node> > CreateTrees(AdjacencyList list) { List <List <Node> > trees = new List <List <Node> >(); trees.Add(new List <Node>() { list.List[0] }); for (int i = 1; i < list.List.Count; ++i) { Node currentNode = list.List[i]; for (int j = 0; j < trees.Count; ++j) { List <Node> tree = null; foreach (Node node in trees[j]) { bool hasRoute = false; HasRouteTo(currentNode, node, new List <Node>(), ref hasRoute); if (hasRoute) { tree = trees[j]; break; } } if (tree != null) { tree.Add(currentNode); break; } else if (j == trees.Count - 1 || trees == null) { trees.Add(new List <Node>() { currentNode }); break; // Don't really need this break } } } return(trees); }
public static bool SolveShortest(AdjacencyList list, out string path) { path = ""; List <AdjacentNode> listPath = new List <AdjacentNode>(); for (int i = 0; i < list.List.Count; ++i) { if (list.List[i].Tag == list.Start) { TraverseShortest(new AdjacentNode(list.List[i], 0.0f), new List <AdjacentNode>(), ref listPath, list.End); break; } } path = Node.GetStringFromPath(listPath); return(!string.IsNullOrEmpty(path)); }
public static void BuildKruskalMST(AdjacencyList list, out string result) { List <List <Node> > trees = CreateTrees(list); List <List <Edge> > mstTrees = CreateEdgesForTrees(trees); foreach (var edges in mstTrees) { SortEdges(edges); } List <List <Edge> > chosenEdges = new List <List <Edge> >(); for (int i = 0; i < mstTrees.Count; ++i) { List <Edge> tree = mstTrees[i]; List <Edge> chosen = new List <Edge>(); chosenEdges.Add(chosen); TraverseMST(mstTrees[i][0], 0, ref tree, ref chosen); } //foreach (var tree in chosenEdges) //{ // foreach (var edge in tree) // { // Console.WriteLine(edge); // } //} List <string> hubs = new List <string>(); foreach (var tree in chosenEdges) { GetHubPlacements(ref hubs, tree); } string output = MSTTreesToString(chosenEdges, hubs); result = output; }
static void ParseMazes(string filePath, List <AdjacencyList> lists) { string[] lines = File.ReadAllLines(filePath); List <string> nodeLines = new List <string>(); for (int i = 0; i < lines.Length; ++i) { if (lines[i] == "" || char.IsLetter(lines[i][0])) { nodeLines.Add(lines[i]); } } AdjacencyList currentMaze = new AdjacencyList(); List <Node> currentNodes = new List <Node>(); int previousMazeCounts = 0; for (int i = 0; i < nodeLines.Count; ++i) { if (i - previousMazeCounts == 0) { string[] nodes = nodeLines[i].Split(','); for (int j = 0; j < nodes.Length; ++j) { Node n = new Node(nodes[j]); currentNodes.Add(n); } } else if (i - previousMazeCounts == 1) { string[] endPoints = nodeLines[i].Split(','); currentMaze.Start = endPoints[0]; currentMaze.End = endPoints[1]; } else { string[] nodes = nodeLines[i].Split(','); Node node = null; for (int x = 0; x < currentNodes.Count; ++x) { if (nodes[0] == currentNodes[x].Tag) { node = currentNodes[x]; } } for (int j = 1; j < nodes.Length; ++j) { for (int x = 0; x < currentNodes.Count; ++x) { if (nodes[j] == currentNodes[x].Tag) { node.AddAdjacent(currentNodes[x], 1.0); } } } if (node != null) { currentMaze.List.Add(node); } } if (nodeLines[i] == "" || i >= nodeLines.Count - 1) { if (currentMaze.List.Count > 0) { previousMazeCounts = i + 1; lists.Add(currentMaze.Clone()); currentMaze.List.Clear(); currentNodes.Clear(); } } } }