public void reachComponent()
        {
            //generate component groups for edges and vertices.
            List<EdgeID> edgeList =edgeLabel;//the list of edges.
            Dictionary<int, String> vertexList = vertexLabel;
            //will show the components it belong to.
            //First build ajacency list, then for each vertex in the vertex list, use the ajacency list to do search until no one can go out.
            //vertexList starts from 0(as we have done refreshList).
            List<List<int>> aList = new List<List<int>>();
            foreach (int key in vertexList.Keys)
            {
                List<int> aListkey = new List<int>();
                aList.Add(aListkey);
            }
            foreach (EdgeID el in edgeList)//build the ajacency list.
            {
                int id1 = el.ID1;
                int id2 = el.ID1;
                aList[id1].Add(id2);
                aList[id2].Add(id1);
            }
            List<List<int>> components = new List<List<int>>();
            bool[] visited = new bool[aList.Count];//number of vertices, record if they are used or not.

            for (int i = 0; i < visited.Length; i++)
            {
                if (visited[i]) continue;

                List<int> component = new List<int>();
                component.Add(i);
                //travesal of graph using graph search.
                Stack idStack = new Stack();
                idStack.Push(i);
                while (idStack.Count != 0)
                {//currently possible duplicate of work because there are cycles.
                    int currrentID = (int)idStack.Pop();
                    if (visited[currrentID]) continue;
                    visited[currrentID] = true;
                    foreach (int nextID in aList[currrentID])
                    {
                        if (!component.Contains(nextID))//now it's OK;
                        {
                            component.Add(nextID);
                            idStack.Push(nextID);
                        }

                    }
                }
                components.Add(component);
            }
            compCount = components.Count;
            //vertexList and edgeList;
            //need to clear the dictionaries before adding new entries.
            compEdgeList = new Dictionary<int, List<EdgeID>>();
            compVertexList = new Dictionary<int, List<VertexID>>();
            for (int i = 0; i < compCount; i++)
            {
                List<int> sepCompVer = components[i];
                List<VertexID> sepVerList = new List<VertexID>();
                List<EdgeID> sepEdgeList = new List<EdgeID>();
                for (int j = 0; j < sepCompVer.Count; j++)
                {
                    VertexID vIDpair = new VertexID(sepCompVer[j], vertexLabel[sepCompVer[j]]);
                    sepVerList.Add(vIDpair);
                    foreach(EdgeID edgeIDpair in edgeLabel){
                        if (edgeIDpair.ID1==vIDpair.ID||edgeIDpair.ID2==vIDpair.ID && !sepEdgeList.Contains(edgeIDpair))
                        {
                            sepEdgeList.Add(edgeIDpair);//two nodes of an edge must be in the same component.
                        }
                    }
                }
                compEdgeList.Add(i,sepEdgeList);
                compVertexList.Add(i,sepVerList);
            }

            //pathlist
            foreach (EdgeID pathIDpair in pathLabel)
            {
                int compID1 = 0;
                int compID2 = 1;
                int nodeID1=pathIDpair.ID1;
                int nodeID2=pathIDpair.ID2;
                for (int i = 0; i < compCount; i++)
                {
                    if (components[i].Contains(nodeID1)) compID1 = i;
                    if (components[i].Contains(nodeID2)) compID2 = i;//two nodes of a path might be in the same component.
                }
                compPathList.Add(new CompPathID(compID1, nodeID1, compID2, nodeID2, pathIDpair.LabelOfEdge));
            }
        }
Exemple #2
0
        public void reachComponent()
        {                                                    //generate component groups for edges and vertices.
            List <EdgeID>            edgeList   = edgeLabel; //the list of edges.
            Dictionary <int, String> vertexList = vertexLabel;
            //will show the components it belong to.
            //First build ajacency list, then for each vertex in the vertex list, use the ajacency list to do search until no one can go out.
            //vertexList starts from 0(as we have done refreshList).
            List <List <int> > aList = new List <List <int> >();

            foreach (int key in vertexList.Keys)
            {
                List <int> aListkey = new List <int>();
                aList.Add(aListkey);
            }
            foreach (EdgeID el in edgeList)//build the ajacency list.
            {
                int id1 = el.ID1;
                int id2 = el.ID1;
                aList[id1].Add(id2);
                aList[id2].Add(id1);
            }
            List <List <int> > components = new List <List <int> >();

            bool[] visited = new bool[aList.Count];//number of vertices, record if they are used or not.

            for (int i = 0; i < visited.Length; i++)
            {
                if (visited[i])
                {
                    continue;
                }

                List <int> component = new List <int>();
                component.Add(i);
                //travesal of graph using graph search.
                Stack idStack = new Stack();
                idStack.Push(i);
                while (idStack.Count != 0)
                {//currently possible duplicate of work because there are cycles.
                    int currrentID = (int)idStack.Pop();
                    if (visited[currrentID])
                    {
                        continue;
                    }
                    visited[currrentID] = true;
                    foreach (int nextID in aList[currrentID])
                    {
                        if (!component.Contains(nextID))//now it's OK;
                        {
                            component.Add(nextID);
                            idStack.Push(nextID);
                        }
                    }
                }
                components.Add(component);
            }
            compCount = components.Count;
            //vertexList and edgeList;
            //need to clear the dictionaries before adding new entries.
            compEdgeList   = new Dictionary <int, List <EdgeID> >();
            compVertexList = new Dictionary <int, List <VertexID> >();
            for (int i = 0; i < compCount; i++)
            {
                List <int>      sepCompVer  = components[i];
                List <VertexID> sepVerList  = new List <VertexID>();
                List <EdgeID>   sepEdgeList = new List <EdgeID>();
                for (int j = 0; j < sepCompVer.Count; j++)
                {
                    VertexID vIDpair = new VertexID(sepCompVer[j], vertexLabel[sepCompVer[j]]);
                    sepVerList.Add(vIDpair);
                    foreach (EdgeID edgeIDpair in edgeLabel)
                    {
                        if (edgeIDpair.ID1 == vIDpair.ID || edgeIDpair.ID2 == vIDpair.ID && !sepEdgeList.Contains(edgeIDpair))
                        {
                            sepEdgeList.Add(edgeIDpair);//two nodes of an edge must be in the same component.
                        }
                    }
                }
                compEdgeList.Add(i, sepEdgeList);
                compVertexList.Add(i, sepVerList);
            }

            //pathlist
            foreach (EdgeID pathIDpair in pathLabel)
            {
                int compID1 = 0;
                int compID2 = 1;
                int nodeID1 = pathIDpair.ID1;
                int nodeID2 = pathIDpair.ID2;
                for (int i = 0; i < compCount; i++)
                {
                    if (components[i].Contains(nodeID1))
                    {
                        compID1 = i;
                    }
                    if (components[i].Contains(nodeID2))
                    {
                        compID2 = i;                                 //two nodes of a path might be in the same component.
                    }
                }
                compPathList.Add(new CompPathID(compID1, nodeID1, compID2, nodeID2, pathIDpair.LabelOfEdge));
            }
        }