Пример #1
0
        public MultiGraphNode FindSet(MultiGraphNode x)
        {
            if (Parent[x] != x)
            {
                Parent[x] = FindSet(Parent[x]);
            }

            return(Parent[x]);
        }
Пример #2
0
        public void Union(MultiGraphNode x, MultiGraphNode y)
        {
            MultiGraphNode representativeX = FindSet(x);
            MultiGraphNode representativeY = FindSet(y);

            if (Rank[representativeX] == Rank[representativeY])
            {
                Rank[representativeY]++;
                Parent[representativeX] = representativeY;
            }

            if (Rank[representativeX] > Rank[representativeY])
            {
                Parent[representativeY] = representativeX;
            }
            else
            {
                Parent[representativeX] = representativeY;
            }
        }
Пример #3
0
        public void AddEdge(int vertex1, int vertex2, int edgeWeight)
        {
            MultiGraphNode        node1   = new MultiGraphNode(vertex1);
            MultiGraphNode        node2   = new MultiGraphNode(vertex2);
            Edge <MultiGraphNode> newEdge = new Edge <MultiGraphNode>(node1, node2, edgeWeight);

            if (!Nodes.ContainsKey(vertex1))
            {
                Nodes.Add(vertex1, node1);
            }

            if (!Nodes.ContainsKey(vertex2))
            {
                Nodes.Add(vertex2, node2);
            }

            Nodes[vertex1].Connections.Add(newEdge);
            Nodes[vertex2].Connections.Add(newEdge);
            Edges.Add(newEdge);
        }
Пример #4
0
        /// <summary>
        /// Generic BFS function. Can be used to solve any problem that requires BFS, just plug in the inspectionFunc.
        /// </summary>
        /// <param name="inspectFunc">Must return true if the search is completed and no more nodes need to be checked</param>
        /// <param name="startId">Starting node, by default it is 0.</param>

        public void SetComponenets(bool countComponents)
        {
            if (Nodes.Count == 0)
            {
                return;
            }

            Queue <MultiGraphNode> nodeQueue = new Queue <MultiGraphNode>();

            foreach (var node in Nodes)
            {
                if (!node.Value.IsVisited)
                {
                    if (countComponents)
                    {
                        NumberOfComponents++;
                    }
                    nodeQueue.Enqueue(node.Value);
                    node.Value.IsVisited      = true;
                    node.Value.ComponentIndex = NumberOfComponents;
                    while (nodeQueue.Count != 0)
                    {
                        MultiGraphNode currentNode = nodeQueue.Dequeue();

                        foreach (var connection in currentNode.Connections)
                        {
                            if (!connection.Node2.IsVisited)
                            {
                                connection.Node2.IsVisited      = true;
                                connection.Node2.ComponentIndex = NumberOfComponents;

                                nodeQueue.Enqueue(connection.Node1);
                            }
                        }
                    }
                }
            }
        }
Пример #5
0
 public int FindRank(MultiGraphNode x)
 {
     return(Rank[x]);
 }
Пример #6
0
 public MultiGraphNode FindImmediateParent(MultiGraphNode x)
 {
     return(Parent[x]);
 }
Пример #7
0
 public void MakeSet(MultiGraphNode x)
 {
     Parent[x] = x;
     Rank[x]   = 0;
 }