示例#1
0
 public BFSPaths(Graph g, int s)
 {
     _marked = new bool[g.V];
     _edgeTo = new int[g.V];
     _s = s;
     bfs(g, s);
 }
示例#2
0
 public static int NumberOfSelfLoops(Graph g)
 {
     int count = 0;
     g.V.Times(v => {
         foreach (int w in g.Adjacent(v)) {
             if (v == w) count++;
         }
     });
     return count / 2;
 }
示例#3
0
 public static int MaxDegree(Graph g)
 {
     int max = 0;
     g.V.Times(v => {
         if (Degree(g, v) > max) {
             max = Degree(g, v);
         }
     });
     return max;
 }
示例#4
0
 private void dfs(Graph g, int v)
 {
     _marked[v] = true;
     foreach (var adj in g.Adjacent(v)) {
         if (!_marked[adj]) {
             _edgeTo[adj] = v;
             dfs(g, adj);
         }
     }
 }
示例#5
0
 private void bfs(Graph g, int s)
 {
     var queue = new Queue<int>();
     _marked[s] = true;
     queue.Enqueue(s);
     while(queue.Count>0) {
         int v = queue.Dequeue();
         foreach(int w in g.Adjacent(v)) {
             if(!_marked[w]) {
                 _edgeTo[w] = v;
                 _marked[w] = true;
                 queue.Enqueue(w);
             }
         }
     }
 }
示例#6
0
 static void Main(string[] args)
 {
     Graph g = new Graph(9);
     g.AddEdge(0, 1);
     g.AddEdge(0, 3);
     g.AddEdge(1, 2);
     g.AddEdge(1, 4);
     g.AddEdge(2, 5);
     g.AddEdge(3, 4);
     g.AddEdge(3, 6);
     g.AddEdge(4, 5);
     g.AddEdge(4, 7);
     g.AddEdge(5, 8);
     g.AddEdge(6, 7);
     g.AddEdge(7, 8);
     var p = new BFSPaths(g, 0);
     p.PathTo(8);
 }
示例#7
0
 public static int Degree(Graph g, int v)
 {
     return g.Adjacent(v).Count();
 }
示例#8
0
 public static int AverageDegree(Graph g)
 {
     return 2 * g.E / g.V;
 }