示例#1
0
        public void TestGraph()
        {
            Random rand = new Random();
            int v = rand.Next(10, 30);

            Graph g = new Graph(v);
            int e0 = rand.Next(10, 30);
            string strs = string.Empty;
            for (int i = 0; i < e0; i++)
            {
                int v1 = rand.Next(0, v);
                int v2 = rand.Next(0, v);
                string str = v1 + " - " + v2 + ",";
                string str1 = v2 + " - " + v1 + ",";

                if (v1 == v2 || strs.Contains(str) || strs.Contains(str1))
                {
                    i--;
                    continue;
                }

                g.AddEdge(v1, v2);
                strs += str;
            }

            Debug.WriteLine("input e: " + strs);
            Debug.WriteLine(g.ToString());

            Graph g1 = new Graph(g);
            Debug.WriteLine("g1: ");
            Debug.WriteLine(g1.ToString());
        }
示例#2
0
        /// <summary>
        /// Computes the shortest path between any one of the source vertices in sources
        /// </summary>
        /// <param name="g"></param>
        /// <param name="s"></param>
        public BreadthFirstPaths(Graph g, int s)
        {
            this.Marked = new bool[g.V];
            this._edgeTo = new int[g.V];
            this._s = s;

            this.bfs(g, s);
        }
示例#3
0
文件: CC.cs 项目: qizl/Algorithms
 private void dfs(Graph g, int v)
 {
     this.Marked[v] = true;
     this.ID[v] = this.Count;
     foreach (int w in g.Adj[v])
         if (!this.Marked[w])
             this.dfs(g, w);
 }
示例#4
0
        private void dfs(Graph g, int v)
        {
            this.Marked[v] = true;

            foreach (int w in g.Adj[v])
                if (!this.Marked[w])
                {
                    this._edgeTo[w] = v;
                    this.dfs(g, w);
                }
        }
示例#5
0
文件: CC.cs 项目: qizl/Algorithms
 /// <summary>
 /// Computes the connected components of the undirected graph G.
 /// </summary>
 /// <param name="g"></param>
 public CC(Graph g)
 {
     this.Marked = new bool[g.V];
     this.ID = new int[g.V];
     for (int v = 0; v < g.V; v++)
         if (!this.Marked[v])
         {
             this.dfs(g, v);
             this.Count++;
         }
 }
示例#6
0
文件: Graph.cs 项目: qizl/Algorithms
 public Graph(Graph g)
     : this(g.V)
 {
     this.E = g.E;
     for (int v = 0; v < g.V; v++)
     {
         Part1.Stack<int> reverse = new Part1.Stack<int>();
         foreach (int w in g.Adj[v])
             reverse.Push(w);
         foreach (int w in reverse)
             this.Adj[v].Add(w);
     }
 }
示例#7
0
        private void bfs(Graph g, int s)
        {
            this.Marked[s] = true;
            Queue<int> queue = new Queue<int>();
            queue.Enqueue(s);

            while (queue.Count != 0)
            {
                int v = queue.Dequeue();
                foreach (int w in g.Adj[s])
                    if (!this.Marked[w])
                    {
                        this._edgeTo[w] = s;
                        this.Marked[w] = true;
                        queue.Enqueue(w);
                    }
            }
        }
示例#8
0
        /// <summary>
        /// Initializes a graph from a string using the specified delimiter.
        /// </summary>
        /// <param name="str"></param>
        /// <param name="delimiter"></param>
        public SymbolGraph(string str, char delimiter)
        {
            this._dics = new Dictionary<string, int>();

            // First pass builds the index by reading strings to associate distinct strings with an index
            string[] strs = str.Split(delimiter);
            for (int i = 0; i < strs.Length; i++)
                if (!string.IsNullOrEmpty(strs[i]))
                    if (!this._dics.ContainsKey(strs[i]))
                        this._dics.Add(strs[i], this._dics.Count);

            // inverted index to get string keys in an aray
            this._keys = new string[this._dics.Count];
            Array.Copy(this._dics.Keys.ToArray(), this._keys, this._dics.Count);

            // second pass builds the graph by connecting first vertex on each line to all others
            this.Graph = new Graph(this._dics.Count);
            for (int i = 0; i < strs.Length; i++)
                if (!string.IsNullOrEmpty(strs[i]))
                    this.Graph.AddEdge(this._dics[strs[i]], this._dics[strs[++i]]);
        }