コード例 #1
0
        private CyclePath <TVertex, TEdge> BuildCycle(
            IDictionary <Vertex <TVertex, TEdge>, Edge <TVertex, TEdge> > alreadyVisited,
            Vertex <TVertex, TEdge> vertex)
        {
            var  path        = new List <Edge <TVertex, TEdge> >();
            bool startAdding = false;

            foreach (var visited in alreadyVisited)
            {
                if (visited.Key.Equals(vertex))
                {
                    startAdding = true;
                }
                if (startAdding)
                {
                    path.Add(visited.Value);
                }
            }

            var newCycle = new CyclePath <TVertex, TEdge>(path);

            foreach (var foundCycle in _foundCyles)
            {
                if (foundCycle.Equals(newCycle))
                {
                    return(null);
                }
            }
            _foundCyles.Add(newCycle);
            return(newCycle);
        }
コード例 #2
0
        private List <string> GetCycle(List <Node> nodeList)
        {
            List <NodeCycle> tempNode = new List <NodeCycle>();
            List <Node>      lista    = nodeList;

            for (int i = 0; i < lista.Count; i++)
            {
                NodeCycle nodzik = new NodeCycle(lista[i].node.ToString());
                tempNode.Add(nodzik);
            }
            int gf;

            for (int i = 0; i < tempNode.Count; i++)
            {
                gf = i;
                string thisName = tempNode[i].Id;
                var    tn       = lista.Find(a => a.node.ToString() == thisName);

                if (tn.nodesLinked.Count > 0)
                {
                    for (int x = 0; x < tn.nodesLinked.Count; x++)
                    {
                        if (tn.node == 0)
                        {
                            if (tn.nodesLinked[x] == 1)
                            {
                            }
                            else
                            {
                                string connectWith = tn.nodesLinked[x].ToString();

                                var z = tempNode.Find(a => a.Id == connectWith);
                                tempNode[i].Targets.Add(z);
                            }
                        }
                        else if (gf + 1 == tempNode.Count)
                        {
                            string connectWith = tn.nodesLinked[x].ToString();

                            var z = new NodeCycle("1");
                            tempNode[i].Targets.Add(z);
                        }
                        else
                        {
                            string connectWith = tn.nodesLinked[x].ToString();

                            var z = tempNode.Find(a => a.Id == connectWith);
                            tempNode[i].Targets.Add(z);
                        }
                    }
                }
            }
            StringBuilder cycles    = new StringBuilder();
            List <string> cycleList = new List <string>();

            foreach (var cycle in CyclePath.FindSimpleCycles(tempNode[0]))
            {
                Console.WriteLine(string.Join(",", cycle.Select(n => n.Id)));
                foreach (var node in cycle)
                {
                    cycles.Append(node.Id + " ");
                }
                var cyclesStringified = cycles.ToString();
                cycleList.Add(cyclesStringified.Trim());
                cycles.Clear();
            }
            return(cycleList);
        }