public static Solution branchandBound(BasicGraph <int> graph) { int?[,] distMatrix = generateDistMatrix(graph); int size = graph.AllNodes.Count; var possibleSolutions = new List <Solution>(); int?upperBound = null; int lowerBound = reduce(distMatrix, size); Solution solutionRoot = new Solution(0, distMatrix); solutionRoot.minCost = lowerBound; solutionRoot.size = size; solutionRoot.isLeaf = false; var solutions = new LinkedList <Solution>(); upperBound = findSolution(solutionRoot, solutions, size); while (solutions.Count > 1) { int?nextSolution = findSolution(nextBest(solutions, upperBound), solutions, size); if (nextSolution < upperBound) { upperBound = nextSolution; } } return(solutions.First.Value); }
public void addConnection(GraphNode <T> connectingNode, int weight) { Connections.Add(new GraphEdge <T>(connectingNode, weight)); connectingNode.Connections.Add(new GraphEdge <T>(this, weight)); if (isNew) { isNew = false; this.MemberOf = connectingNode.MemberOf; } }
static int?[,] generateDistMatrix(BasicGraph <int> graph) { int graphSize = graph.AllNodes.Count; var distMatrix = new int?[graphSize, graphSize]; //set all values to null for (int i = 0; i < graphSize; i++) { for (int j = 0; j < graphSize; j++) { distMatrix[i, j] = null; } } //create distance matrix for (int i = 0; i < graphSize; i++) { foreach (GraphEdge <int> edge in graph.AllNodes[i].Connections) { distMatrix[i, graph.AllNodes.IndexOf(edge.ConnectingNode)] = edge.Weight; } } return(distMatrix); }
public GraphNode(T data) { Data = data; MemberOf = null; }
public GraphNode() { MemberOf = null; }