Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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;
            }
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
 public GraphNode(T data)
 {
     Data     = data;
     MemberOf = null;
 }
Exemplo n.º 5
0
 public GraphNode()
 {
     MemberOf = null;
 }