Beispiel #1
0
        private void Dfs(Graph g, int v)
        {
            Marked[v] = true;
            var adjs = g.Adj(v).ToArray();

            foreach (int w in g.Adj(v))
            {
                if (!Marked[w])
                {
                    EdgeTo[w] = v;
                    Dfs(g, w);
                }
            }
        }
Beispiel #2
0
        public void Girth()
        {
            Dictionary <int, Dictionary <int, int> > bfsPaths = new Dictionary <int, Dictionary <int, int> >();

            for (int inx = 0; inx < graph.V; inx++)
            {
                var bfsPath = new BfsPaths(graph, inx);
                Console.WriteLine(string.Format("{0}:{1}", inx, bfsPath));
                bfsPaths.Add(inx, bfsPath.edgeTo);
            }

            for (int v = 0; v < graph.V; v++)
            {
                IList <int> adjs      = graph.Adj(v);
                int         adjsCount = adjs.Count;
                if (adjsCount == 0 || adjsCount == 1)
                {
                    continue;                                    // in case of isolated node or node with one connection
                }
                // here we know that v has at least 2 node adjacent to it
                for (int inx = 0; inx < adjsCount; inx++)
                {
                    List <int> adjNodeIds = new List <int>(adjs); adjNodeIds.RemoveAt(inx);
                    FindCircle(adjs[inx], adjNodeIds, bfsPaths, v);
                }
            }
        }
Beispiel #3
0
        private void Dfs(Graph g, int v)
        {
            Marked[v] = true;
            Count    += 1;

            Debug.WriteLine(v);

            foreach (int w in g.Adj(v))
            {
                if (!Marked[w])
                {
                    Dfs(g, w);
                }
            }
        }
Beispiel #4
0
        private void Dfs(Graph g, int v, int u)
        {
            visited[v] = true;

            foreach (int w in g.Adj(v))
            {
                if (!visited[w])
                {
                    Dfs(g, w, v);
                }
                else if (w != u)
                {
                    HasCycle = true;
                    return;
                }
            }
        }
Beispiel #5
0
        private void Bfs(Graph g, int s)
        {
            Queue <int> queue = new Queue <int>();

            marked[s] = true;
            queue.Enqueue(s);

            while (queue.Count != 0)
            {
                int v = queue.Dequeue();
                foreach (int w in g.Adj(v))
                {
                    if (!marked[w])
                    {
                        marked[w] = true;
                        edgeTo[w] = v;
                        queue.Enqueue(w);
                    }
                }
            }
        }
Beispiel #6
0
        private void Dfs(Graph g, int s)
        {
            Stack <int> stack = new Stack <int>();

            marked[s] = true;
            id[s]     = count;
            stack.Push(s);

            while (stack.Count != 0)
            {
                int v = stack.Pop();

                foreach (int w in g.Adj(v))
                {
                    if (!marked[w])
                    {
                        marked[w] = true;
                        id[w]     = count;
                        stack.Push(w);
                    }
                }
            }
        }