public Graph GetTree()
        {
            // initialize the graph that will hold the MST
            Graph mst = new KevinGraph();

            // initialize the priority queue that will hold the vertices outside the MST
            PriorityQueue remainingVertices = new KevinPriorityQueue();

            foreach (GraphVertex v in weightedGraph.GetAllVertices())
            {
                remainingVertices.Enqueue(new MSTPriorityQueueNode(data: v, priority: int.MaxValue));
            }

            Dictionary <string, GraphEdge> lowestCostEdgeForVertex = new Dictionary <string, GraphEdge>();

            while (remainingVertices.Count() > 0)
            {
                // Get the vertex with the lowest code to add to the MST
                // The first vertex is chosen arbitrarily because all vertices start with max cost.
                PriorityQueueNode currentNode   = remainingVertices.Dequeue();
                GraphVertex       currentVertex = currentNode.Data as GraphVertex;

                // Add the vertex and its lowest cost edge (if any) to the MST
                mst.AddVertex(currentVertex.UniqueKey);
                if (lowestCostEdgeForVertex.ContainsKey(currentVertex.UniqueKey))
                {
                    GraphEdge lowestCostEdge = lowestCostEdgeForVertex[currentVertex.UniqueKey];
                    // TO-DO: why?
                    mst.AddEdge(lowestCostEdge.SourceVertexUniqueKey, lowestCostEdge.TargetVertexUniqueKey, lowestCostEdge.Weight);
                    mst.AddEdge(lowestCostEdge.TargetVertexUniqueKey, lowestCostEdge.SourceVertexUniqueKey, lowestCostEdge.Weight);
                }

                // update the minimum cost for each adjacent vertex, and the associated edge
                foreach (GraphEdge edge in currentVertex.GetIncidentEdges())
                {
                    GraphVertex edgeTarget = weightedGraph.GetVertexByUniqueKey(edge.TargetVertexUniqueKey);

                    if (!remainingVertices.Contains(edgeTarget.UniqueKey))
                    {
                        continue;
                    }

                    int newCost = edge.Weight;
                    PriorityQueueNode targetNode = remainingVertices.Peek(edgeTarget.UniqueKey);
                    if (newCost < targetNode.Priority)
                    {
                        remainingVertices.ChangePriority(targetNode.Data.UniqueKey, newCost);
                        lowestCostEdgeForVertex[edgeTarget.UniqueKey] = edge;
                    }
                }
            }

            return(mst);
        }
示例#2
0
        public static Graph FromString(string s)
        {
            Dictionary <string, KevinGraphVertex> nameToVertexMap = new Dictionary <string, KevinGraphVertex>();

            KevinGraph g = new KevinGraph();

            if (string.IsNullOrEmpty(s))
            {
                return(g);
            }

            using (StringReader sr = new StringReader(s))
            {
                while (true)
                {
                    string currentLine = sr.ReadLine();
                    if (currentLine == null)
                    {
                        break;
                    }

                    string[] vertices = currentLine.Split(new char[] { ' ' });
                    if (vertices.Length == 0)
                    {
                        continue;
                    }

                    KevinGraphVertex vertex = CreateOrGetVertexByName(g, vertices[0], nameToVertexMap);
                    if (!g.ContainsVertex(vertex.UniqueKey))
                    {
                        g.AddVertex(vertex.UniqueKey);
                    }

                    for (int i = 1; i < vertices.Length; i++)
                    {
                        GraphEdge edge = ParseEdge(g, vertex, vertices[i], nameToVertexMap);
                        string    targetVertexUniqueKey = edge.TargetVertexUniqueKey;
                        if (!g.ContainsVertex(targetVertexUniqueKey))
                        {
                            g.AddVertex(targetVertexUniqueKey);
                        }

                        g.AddEdge(edge.SourceVertexUniqueKey, edge.TargetVertexUniqueKey, edge.Weight);
                    }
                }

                return(g);
            }
        }
示例#3
0
        private KevinGraph(KevinGraph source)
        {
            this.vertices = new List <KevinGraphVertex>();
            foreach (GraphVertex v in source.vertices)
            {
                this.AddVertex(v.UniqueKey);
            }

            foreach (GraphVertex v in source.vertices)
            {
                foreach (GraphEdge edge in v.GetIncidentEdges())
                {
                    this.AddEdge(edge.SourceVertexUniqueKey, edge.TargetVertexUniqueKey, edge.Weight);
                }
            }
        }