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); }
/// <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); } }
/// <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>()); } }