示例#1
0
        public static int degree(Graph G, int v)
        {
            int degree = 0;

            foreach (int w in G.adj(v))
            {
                degree++;
            }

            return(degree);
        }
示例#2
0
 private void dfs(Graph G, int v)
 {
     mark[v] = true;
     foreach (int w in G.adj(v))
     {
         if (!mark[w])
         {
             edgeTo[w] = v;
             dfs(G, w);
         }
     }
 }
示例#3
0
        public static int numberOfSelfLoops(Graph G)
        {
            int count = 0;

            for (int i = 0; i < G.v(); i++)
            {
                foreach (int j in G.adj(i))
                {
                    if (i == j)
                    {
                        count++;
                    }
                }
            }

            return(count / 2);
        }
示例#4
0
        private void bfs(Graph G, int s)
        {
            Queue <int> q = new Queue <int>();

            mark[s] = true;
            q.enqueue(s);
            while (!q.isEmpty())
            {
                int v = q.dequeue();
                foreach (int w in G.adj(v))
                {
                    if (!mark[w])
                    {
                        edgeTo[w] = v;
                        mark[w]   = true;
                        q.enqueue(w);
                    }
                }
            }
        }