示例#1
0
 /// <summary>
 /// szukanie cykli na grafie przy uzyciu kolorowanie wierzch. na bialo/szaro/czarno
 /// </summary>
 /// <param name="x"></param>
 /// <param name="white"></param>
 /// <param name="grey"></param>
 /// <param name="black"></param>
 /// <param name="cyc">lista z cyklami, ktorych szukamy</param>
 /// <param name="temp">lista z aktualnym cyklem</param>
 /// <param name="elem">aktualny wierzcholek,w ktorym jestesmy</param>
 private static void rekc(DirectedGraphList x, List <int> white, List <int> grey, List <int> black, List <List <int> > cyc, List <int> temp, int elem)
 {
     if (black.Contains(elem))
     {
         return;
     }
     if (white.Contains(elem))
     {
         white.Remove(elem);
         grey.Add(elem);
     }
     temp.Add(elem);
     for (int i = 0; i < x.GetConnections(elem).Count; i++)
     {
         if (grey.Contains(x.GetConnections(elem)[i]))//znaleziono cykl
         {
             temp.Add(x.GetConnections(elem)[i]);
             int ind = cyc.Count;
             cyc.Add(new List <int>());
             for (int j = 0; j < temp.Count; j++)
             {
                 cyc[ind].Add(temp[j]);
             }
             temp.Remove(x.GetConnections(elem)[i]);
         }
         if (white.Contains(x.GetConnections(elem)[i]))//bialy / wchodzimy
         {
             rekc(x, white, grey, black, cyc, temp, x.GetConnections(elem)[i]);
         }
     }
     temp.Remove(elem);
     grey.Remove(elem);
     black.Add(elem);
 }
示例#2
0
        public static DirectedGraphMatrixInc ConvertToSMatrixInc(DirectedGraphList from)
        {
            int sumc = 0;

            for (int i = 0; i < from.NodesNr; i++)
            {
                sumc += from.CountElem(i);
            }
            int c = 0;
            DirectedGraphMatrixInc x = new DirectedGraphMatrixInc(from.NodesNr, sumc);

            for (int i = 0; i < from.NodesNr; i++)//pobiera po kolei elementy, dodaje do matrixinc i usuwa z listy
            {
                for (int j = 0; j < from.NodesNr; j++)
                {
                    if (from.GetConnection(i, j))
                    {
                        x.setWeight(i, j, from.getWeight(i, j));
                        x.MakeConnection(i, j, c);
                        c++;
                        from.RemoveConnection(i, j);
                    }
                }
            }
            return(x);
        }
示例#3
0
        /// <summary>
        /// Tworzy Liste wszystkich spojnych skladowych
        /// </summary>
        /// <param name="f">graf</param>
        /// <returns>lista spojnych</returns>
        public static List <List <int> > spojne(DirectedGraphMatrix f)
        {
            List <int> stark = new List <int>();

            bool[] visited = new bool[f.NodesNr];
            visited[0] = true;
            DirectedGraphList q = Converter.ConvertToSList(f);

            for (int i = 0; i < f.NodesNr; i++)
            {
                rek(q, i, stark, visited);
            }
            q = Converter.ConvertToSList(transpose(f));
            stark.Reverse();
            //tworzyc listy spojnosci OK
            List <List <int> > lista = new List <List <int> >();

            lista.Add(new List <int>());
            int ind = 0;

            bool[] visited2 = new bool[f.NodesNr];
            for (int i = 0; i < f.NodesNr; i++)
            {
                rek2(q, i, stark, visited2, lista, ref ind);
            }
            lista.Remove(lista[ind]);
            return(lista);
        }
示例#4
0
        /// <summary>
        /// Szukanie cykli na grafie
        /// </summary>
        /// <param name="f">graf</param>
        /// <returns>Lista cykli(lista)</returns>
        public static List <List <int> > circuts(DirectedGraphMatrix f)
        {
            DirectedGraphList  li    = Converter.ConvertToSList(f);
            List <List <int> > cycle = new List <List <int> >();
            List <int>         white = new List <int>();

            for (int i = 0; i < f.NodesNr; i++)
            {
                white.Add(i);
            }
            List <int> grey  = new List <int>();
            List <int> black = new List <int>();
            List <int> temp  = new List <int>();

            for (int i = 0; i < li.NodesNr; i++)
            {
                rekc(li, white, grey, black, cycle, temp, i);
            }
            //formatowanie listy !!!
            for (int i = 0; i < cycle.Count; i++)
            {
                while (cycle[i][0] != cycle[i][cycle[i].Count - 1])
                {
                    cycle[i].Remove(cycle[i][0]);
                }
            }
            return(cycle);
        }
示例#5
0
 /// <summary>
 /// Algorytm Kosaraju
 /// stark by finish time
 /// </summary>
 /// <param name="x">graf</param>
 /// <param name="elem">element z ktorego zaczynamy przeszukiwanie</param>
 /// <param name="st">stark by finish time list</param>
 /// <param name="vis">lista odwiedzonych wierzcholkow</param>
 private static void rek(DirectedGraphList x, int elem, List <int> st, bool[] vis)
 {
     vis[elem] = true;
     for (int i = 0; i < x.CountElem(elem); i++)
     {
         if (!vis[x.GetConnections(elem)[i]])
         {
             rek(x, x.GetConnections(elem)[i], st, vis);
         }
     }
     if (!st.Contains(elem))
     {
         st.Add(elem);
     }
 }
示例#6
0
        public static DirectedGraphList ConvertToSList(DirectedGraphMatrix from)
        {
            DirectedGraphList x = new DirectedGraphList(from.NodesNr);

            for (int i = 0; i < from.NodesNr; i++)
            {
                for (int j = 0; j < from.NodesNr; j++)
                {
                    if (from.GetConnection(i, j))
                    {
                        x.MakeConnection(i, j);
                    }
                    x.setWeight(i, j, from.getWeight(i, j));
                }
            }
            return(x);
        }
示例#7
0
 /// <summary>
 /// Algorytm Kosaraju cd
 /// odtwarzanie spojnych z listy
 /// </summary>
 /// <param name="x"></param>
 /// <param name="elem">aktualny wierzcholek,w ktorym jestesmy</param>
 /// <param name="st">stark by finish time list</param>
 /// <param name="vis">lista odwiedzonych wierzcholkow</param>
 /// <param name="lista">lista spojnych</param>
 /// <param name="ind">lista o ktorym indeksie jest teraz obslugiwana</param>
 private static void rek2(DirectedGraphList x, int elem, List <int> st, bool[] vis, List <List <int> > lista, ref int ind)
 {
     if (vis[elem])
     {
         st.Remove(elem);
         return;
     }
     vis[elem] = true;
     lista[ind].Add(elem);
     for (int i = 0; i < x.CountElem(elem); i++)
     {
         if (!vis[x.GetConnections(elem)[i]])
         {
             rek2(x, x.GetConnections(elem)[i], st, vis, lista, ref ind);
         }
     }
     if (lista[ind].Count != 0)
     {
         ind++;
         lista.Add(new List <int>());
     }
 }