Ejemplo n.º 1
0
        public HamiltonPath(IAdjacency iAdjacency, int s)
        {
            _iAdjacency = iAdjacency;
            _s          = s;
            _visited    = new bool[_iAdjacency.V];
            _end        = -1;
            _pre        = new int[_iAdjacency.V];

            Dfs(s, s, _iAdjacency.V);
        }
Ejemplo n.º 2
0
        public BfsGraph(IAdjacency iAdjacency)
        {
            _adjacency = iAdjacency;
            _visited   = new bool[_adjacency.V];
            Order      = new List <int>();

            for (int i = 0; i < _adjacency.V; i++)
            {
                if (!_visited[i])
                {
                    Bfs(i);
                }
            }
        }
Ejemplo n.º 3
0
 public StronglyConnectedWeight(IAdjacency iAdjacency)
 {
     _iAdjacency = iAdjacency.Reverse();
     _origion    = iAdjacency;
     _post       = new List <int>();
     _visited    = new bool[iAdjacency.V];
     for (int i = 0; i < iAdjacency.V; i++)
     {
         if (!_visited[i])
         {
             Dfs(i);
         }
     }
     CalculateStronglyConnectedWeight();
 }
Ejemplo n.º 4
0
        public ToopSortByDfs(IAdjacency iAdjacency)
        {
            _iAdjacency = iAdjacency;
            _visited    = new bool[iAdjacency.V];
            Result      = new List <int>();

            for (int v = 0; v < iAdjacency.V; v++)
            {
                if (!_visited[v])
                {
                    Dfs(v);
                }
            }
            Result.Reverse();
        }
Ejemplo n.º 5
0
 /// <summary>
 /// 构造方法
 /// </summary>
 /// <param name="iAdjacency">输入的图的数据结构</param>
 public DfsGraph(IAdjacency iAdjacency)
 {
     _adjacency = iAdjacency;
     _visited   = new int[iAdjacency.V];
     for (int i = 0; i < _visited.Length; i++)
     {
         _visited[i] = -1;
     }
     for (int i = 0; i < iAdjacency.V; i++)
     {
         if (_visited[i] == -1)
         {
             Dfs(i, ConnectedComponentCount++);
         }
     }
 }
Ejemplo n.º 6
0
 public void FindBridge(IAdjacency iAdjacency)
 {
     _iaAdjacency = iAdjacency;
     _visited     = new bool[_iaAdjacency.V];
     _order       = new int[_iaAdjacency.V];
     _low         = new int[_iaAdjacency.V];
     _cnt         = 0;
     BridgeEdge   = new List <Edge>();
     for (int i = 0; i < _iaAdjacency.V; i++)
     {
         if (!_visited[i])
         {
             Dfs(i, i);
         }
     }
 }
Ejemplo n.º 7
0
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="adjacency"></param>
        /// <param name="s"></param>
        /// <param name="t"></param>
        public SingleSourcePath(IAdjacency adjacency, int s, int t)
        {
            _adjacency = adjacency;
            _adjacency.ValidateNumber(s);
            _adjacency.ValidateNumber(t);
            S        = s;
            T        = t;
            PreNodes = new int[_adjacency.V];
            _visited = new bool[_adjacency.V];
            for (int i = 0; i < PreNodes.Length; i++)
            {
                PreNodes[i] = -1;
            }

            Dfs(s, s);
        }
Ejemplo n.º 8
0
        public void FindCutPoint(IAdjacency iAdjacency)
        {
            _iAdjacency = iAdjacency;
            _visited    = new bool[_iAdjacency.V];
            _order      = new int[_iAdjacency.V];
            _low        = new int[_iAdjacency.V];
            _cnt        = 0;
            CutPoints   = new HashSet <int>();

            for (int v = 0; v < iAdjacency.V; v++)
            {
                if (!_visited[v])
                {
                    Dfs(v, v);
                }
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="iAdjacency"></param>
        public CircleDetection(IAdjacency iAdjacency)
        {
            if (iAdjacency.Directed)
            {
                throw new Exception("direced graph can not be support!");
            }
            _adjacency = iAdjacency;
            _visited   = new bool[_adjacency.V];

            for (int i = 0; i < _adjacency.V; i++)
            {
                if (!_visited[i])
                {
                    if (Dfs(i, i))
                    {
                        IsHaveCircle = true;
                        break;
                    }
                }
            }
        }
Ejemplo n.º 10
0
 public DirectedCircleDectection(IAdjacency iAdjacency)
 {
     if (!iAdjacency.Directed)
     {
         throw new Exception("directed graph can be support");
     }
     _iAdjacency = iAdjacency;
     _visited    = new bool[iAdjacency.V];
     _onPath     = new bool[iAdjacency.V];
     for (int i = 0; i < iAdjacency.V; i++)
     {
         if (!_visited[i])
         {
             if (Dfs(i))
             {
                 IsHaveCircle = true;
                 break;
             }
         }
     }
 }
Ejemplo n.º 11
0
 /// <summary>
 /// 构造方法
 /// </summary>
 /// <param name="iAdjacency"></param>
 public BinaryPartitionDetection(IAdjacency iAdjacency)
 {
     _adjacency = iAdjacency;
     _visited   = new bool[_adjacency.V];
     _colors    = new int[_adjacency.V];
     for (int i = 0; i < _colors.Length; i++)
     {
         _colors[i] = -1;
     }
     for (int i = 0; i < _adjacency.V; i++)
     {
         if (!_visited[i])
         {
             if (!Dfs(i, 0))
             {
                 IsBinaryPartition = false;
                 break;
             }
         }
     }
 }
Ejemplo n.º 12
0
        public ToopSort(IAdjacency iAdjacency)
        {
            _iAdjacency = iAdjacency;
            if (!iAdjacency.Directed)
            {
                throw new Exception("directed graph can be support");
            }
            Result = new List <int>();
            Queue <int> queue = new Queue <int>();

            int[] indgree = new int[iAdjacency.V];
            for (int i = 0; i < iAdjacency.V; i++)
            {
                indgree[i] = iAdjacency.InDegree(i);
                if (iAdjacency.InDegree(i) == 0)
                {
                    queue.Enqueue(i);
                }
            }

            while (queue.Count > 0)
            {
                int v = queue.Dequeue();
                Result.Add(v);
                foreach (var w in _iAdjacency.GetAllContiguousEdge(v))
                {
                    indgree[w]--;
                    if (indgree[w] == 0)
                    {
                        queue.Enqueue(w);
                    }
                }
            }

            if (Result.Count != iAdjacency.V)
            {
                IsHaveCircle = true;
                Result.Clear();
            }
        }
Ejemplo n.º 13
0
        public BinaryPartGraphMatch(IAdjacency iAdjacency)
        {
            _iAdjacency = iAdjacency;
            BinaryPartitionDetection binaryPartitionDetection = new BinaryPartitionDetection(iAdjacency);

            if (!binaryPartitionDetection.IsBinaryPartition)
            {
                throw new Exception("you must use two partite graph");
            }
            int[]       colors      = binaryPartitionDetection.Colors;
            WeightGraph weightGraph = new WeightGraph(iAdjacency.V + 2, true);

            for (int v = 0; v < iAdjacency.V; v++)
            {
                if (colors[v] == 0)
                {
                    weightGraph.AddEdge(iAdjacency.V, v, 1);
                }
                if (colors[v] == 1)
                {
                    weightGraph.AddEdge(v, iAdjacency.V + 1, 1);
                }
                foreach (var w in iAdjacency.GetAllContiguousEdge(v))
                {
                    if (colors[v] == 0)
                    {
                        weightGraph.AddEdge(v, w, 1);
                    }
                    else if (colors[v] == 1)
                    {
                        weightGraph.AddEdge(w, v, 1);
                    }
                }
            }
            Edmonds_Karp edmondsKarp = new Edmonds_Karp(weightGraph, iAdjacency.V, iAdjacency.V + 1);

            MaxMatch       = edmondsKarp.MaxFlow;
            IsPerfectMatch = MaxMatch * 2 == iAdjacency.V;
        }
Ejemplo n.º 14
0
 public EulerLoop(IAdjacency iAdjacency)
 {
     _iAdjacency = iAdjacency;
 }