Esempio n. 1
0
        public List <int> JordanCenterBFS()
        {
            int        n    = AdjacencyMatrix.GetLength(0);
            List <int> path = new List <int>();

            path = BFS(0);
            int v1 = path.Last();

            path = BFS(v1);
            int v2 = path.Last();

            if (path.Count % 2 == 1)
            {
                return new List <int>()
                       {
                           path[path.Count / 2]
                       }
            }
            ;
            else
            {
                return new List <int>()
                       {
                           path[path.Count / 2], path[path.Count / 2 - 1]
                       }
            };
        }
Esempio n. 2
0
        /// <summary>
        /// Metoda odpowiedzialna za wizualizację macierzy miast
        /// </summary>
        /// <returns> string przechowujący zwizualizowaną macierz sąsiedztwa </returns>
        public string ShowCities()
        {
            StringBuilder matrixSB = new StringBuilder();

            for (int i = 0; i < AdjacencyMatrix.GetLength(0); i++)
            {
                for (int j = 0; j < AdjacencyMatrix.GetLength(1); j++)
                {
                    if (AdjacencyMatrix[i, j] != INF)
                    {
                        matrixSB.Append(AdjacencyMatrix[i, j].ToString());
                    }
                    else
                    {
                        matrixSB.Append("INF");
                    }
                    matrixSB.Append("  ");
                }

                matrixSB.Append(Environment.NewLine);
            }
            if (_bestDistance > 0)
            {
                matrixSB.Append("Najlepsze rozwiązanie: " + _bestDistance);
            }
            return(matrixSB.ToString());
        }
        public bool FindPath()
        {
            int         end      = NodeCount - 1;
            int         start    = 0;
            Queue <int> bfsQueue = new Queue <int>();

            bfsQueue.Enqueue(start);
            IsChecked[start] = true;
            while (bfsQueue.Any())
            {
                var temp = bfsQueue.Dequeue();
                if (temp == end)
                {
                    return(true);
                }
                for (int i = 0; i < AdjacencyMatrix.GetLength(1); i++)
                {
                    if (AdjacencyMatrix[temp, i] > 0 && !IsChecked[i])
                    {
                        Prevs[i] = temp;
                        bfsQueue.Enqueue(i);
                        IsChecked[i] = true;
                    }
                }
            }
            return(false);
        }
Esempio n. 4
0
        private void UpdateGraph()
        {
            Model = new GraphModel(true);
            for (int i = 0; i < AdjacencyMatrix.GetLength(0); i++)
            {
                Model.AddVertex(new GraphVertex {
                    Number = i
                });
            }

            var vx = Model.Vertices.ToList();

            for (int i = 0; i < AdjacencyMatrix.GetLength(0); i++)
            {
                for (int j = 0; j < AdjacencyMatrix.GetLength(1); j++)
                {
                    if (AdjacencyMatrix[i, j] == 1)
                    {
                        if (Model.ContainsEdge(vx[j], vx[i]))
                        {
                            continue;
                        }
                        Model.AddEdge(new GraphEdge(vx[i], vx[j]));
                    }
                }
            }
        }
Esempio n. 5
0
        private void CalculateBandWidth()
        {
            int maxDifferenceInMatrix = 0;

            for (int i = 0; i < AdjacencyMatrix.GetLength(0); i++)
            {
                int maxDifferenceInRow = 0;

                for (int j = 0; j < AdjacencyMatrix.GetLength(1); j++)
                {
                    for (int k = j + 1; k < AdjacencyMatrix.GetLength(1); k++)
                    {
                        if (Math.Abs(AdjacencyMatrix[i, j] - AdjacencyMatrix[i, k]) > maxDifferenceInRow)
                        {
                            maxDifferenceInRow = Math.Abs(AdjacencyMatrix[i, j] - AdjacencyMatrix[i, k]);
                        }
                    }
                }

                if (maxDifferenceInRow > maxDifferenceInMatrix)
                {
                    maxDifferenceInMatrix = maxDifferenceInRow;
                }
            }

            BandWidth = maxDifferenceInMatrix;
        }
Esempio n. 6
0
        private List <int> HKC(int[] C, int[] newIndex)
        {
            int n = AdjacencyMatrix.GetLength(0);

            int[] height   = new int[n];
            int   diameter = 0;

            for (int i = (n - 1); i > 0; i--)
            {
                var remote    = C[i];
                int newHeight = height[i] + 1;
                int newDiam   = height[remote] + newHeight;

                if (newHeight > height[remote])
                {
                    height[remote] = newHeight;
                }

                if (newDiam > diameter)
                {
                    diameter = newDiam;
                }
            }

            int radiusFloor = diameter / 2;

            List <int> center = new List <int>();

            for (int i = 0; i < n; i++)
            {
                if (height[i] == radiusFloor)
                {
                    if (diameter % 2 == 1)
                    {
                        center.Add(i);
                        center.Add(C[i]);
                    }
                    else
                    {
                        center.Add(i);
                    }
                    break;
                }
            }

            var result = new List <int>();

            for (int i = 0; i < n; i++)
            {
                if (center.Contains(newIndex[i]))
                {
                    result.Add(i);
                }
            }
            return(result);
        }
Esempio n. 7
0
        public int VerticeDegree(int v)
        {
            int degree = 0;

            for (int i = 0; i < AdjacencyMatrix.GetLength(0); i++)
            {
                degree += AdjacencyMatrix[v, i];
            }
            return(degree);
        }
Esempio n. 8
0
        public int FindC3ByMatrixMul3()
        {
            int n = AdjacencyMatrix.GetLength(0);
            var AdjacencyMatrixCubed = ArrayHelper.Multiply(AdjacencyMatrix, ArrayHelper.Multiply(AdjacencyMatrix, AdjacencyMatrix));
            int sum = 0;

            for (int i = 0; i < n; i++)
            {
                sum += AdjacencyMatrixCubed[i, i];
            }
            return(sum / 6);
        }
Esempio n. 9
0
 public void WriteAdjacencyMatrix()
 {
     for (int i = 0; i < AdjacencyMatrix.GetLength(0); i++)
     {
         for (int j = 0; j < AdjacencyMatrix.GetLength(1); j++)
         {
             Console.Write(AdjacencyMatrix[i, j] + "; ");
         }
         Console.WriteLine();
         Console.WriteLine();
     }
 }
Esempio n. 10
0
        public List <Edge> EdgesForVertex(int vertex, bool?colored = null)
        {
            List <Edge> edges = new List <Edge>();

            for (int i = 0; i < AdjacencyMatrix.GetLength(0); i++)
            {
                if (AdjacencyMatrix[i, vertex] == 1 && (colored == null || (ColorMatrix[i, vertex] != 0) == colored))
                {
                    edges.Add(new Edge(i, vertex, ColorMatrix[i, vertex]));
                }
            }
            return(edges);
        }
Esempio n. 11
0
        private void SetupDegreesList()
        {
            int n = AdjacencyMatrix.GetLength(0);

            for (int i = 0; i < n; i++)
            {
                int degree = 0;
                for (int j = 0; j < n; j++)
                {
                    degree += AdjacencyMatrix[i, j];
                }
                Degrees[i] = degree;
            }
        }
Esempio n. 12
0
        public int getNbEndVertexOfLightestEdge(int vertexindex, bool[] isvisited) //zwraca numer wierzchołka do którego koszt dojścia jest najmniejszy i jest jeszcze nieodwiedzony (jeśli nie ma takiego zwraca -1)
        {
            int weight = Int32.MaxValue;
            int nb     = -1;

            for (int a = 0; a < AdjacencyMatrix.GetLength(1); a++)
            {
                if (weight > AdjacencyMatrix[vertexindex, a] && isvisited[a] != true)
                {
                    weight = AdjacencyMatrix[vertexindex, a];
                    nb     = a;
                }
            }
            return(nb);
        }
Esempio n. 13
0
        public void Save(string filename)
        {
            string csv = VerticeCount().ToString() + "\n" + EdgeCount().ToString() + "\n";

            for (int i = 0; i < AdjacencyMatrix.GetLength(0); i++)
            {
                for (int j = i + 1; j < AdjacencyMatrix.GetLength(0); j++)
                {
                    if (AdjacencyMatrix[i, j] == 1)
                    {
                        csv += (i + 1).ToString() + " " + (j + 1).ToString() + "\n";
                    }
                }
            }
            File.WriteAllText(filename, csv.TrimEnd(new char[] { '\r', '\n' }));
        }
Esempio n. 14
0
        public List <Edge> InitializeEdgesInUndirectedGraph(Enums.VerticesType verticesType)
        {
            var neighbourStartedId = verticesType == Enums.VerticesType.Cycle ? 0 : 1;

            for (int vertice = 0; vertice < AdjacencyMatrix.GetLength(0); vertice++)
            {
                for (int neighbour = neighbourStartedId; neighbour < AdjacencyMatrix.GetLength(1); neighbour++)
                {
                    Edges.Add(new Edge(vertice, neighbour, AdjacencyMatrix[vertice, neighbour]));
                }

                neighbourStartedId++;
            }

            return(Edges);
        }
Esempio n. 15
0
        private void CreateAdjacencyList()
        {
            int n = AdjacencyMatrix.GetLength(0);

            AdjacencyList = new List <List <int> >();
            for (int i = 0; i < n; i++)
            {
                AdjacencyList.Add(new List <int>());
                for (int j = 0; j < n; j++)
                {
                    if (AdjacencyMatrix[i, j] > 0 && AdjacencyMatrix[j, i] > 0)
                    {
                        AdjacencyList[i].Add(j);
                    }
                }
            }
        }
Esempio n. 16
0
        public bool FindC3ByMatrixMul()
        {
            int n = AdjacencyMatrix.GetLength(0);
            var AdjacencyMatrixSquare = ArrayHelper.Multiply(AdjacencyMatrix, AdjacencyMatrix);

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (AdjacencyMatrixSquare[i, j] > 0 && AdjacencyMatrix[i, j] > 0)
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Esempio n. 17
0
        public int CountEdges()
        {
            int n       = AdjacencyMatrix.GetLength(0);
            int counter = 0;

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (AdjacencyMatrix[i, j] == 1)
                    {
                        counter++;
                    }
                }
            }

            return(counter / 2);
        }
Esempio n. 18
0
        private List <int> FindCNRecursive(List <int> cycle, int minDegree)
        {
            int n = AdjacencyMatrix.GetLength(0);

            if (cycle.Count >= minDegree + 1)
            {
                int i = cycle.First();
                int j = cycle.Last();
                if (AdjacencyMatrix[i, j] > 0)
                {
                    cycle.Add(i);
                    return(cycle);
                }
            }

            if (cycle.Count > 0) //"cycle" jest narazie ścieżką po kolejnych wierzchołkach (połączonych krawędziami)
            {
                for (int i = 0; i < n; i++)
                {
                    //szukamy wierzchołka który :
                    //- jest połączony z ostatnim wierzchołkiem na liśćie
                    //- nie wystąpił na naszej ścieżce

                    if (AdjacencyMatrix[i, cycle.Last()] > 0 && !cycle.Exists(x => x == i))
                    {
                        cycle.Add(i); //i nie wystąpiło, dodajemy do listy
                        //rekurencja - szukamy kolejnych wierzchołków
                        var currentCycle = FindCNRecursive(cycle, minDegree);
                        //jeśli znajdziemy odpowiedni cykl - zwracamy
                        if (currentCycle.First() == currentCycle.Last())
                        {
                            return(currentCycle);
                        }
                        else //przypadek gdy nie znajdziemy cyklu od tego wierzchołka (z reguły przypadek mostu)
                        {
                            //usuwamy go i rozpoczynamy poszukiwanie od nowego wierzchołka
                            cycle.RemoveAt(0);
                            return(FindCNRecursive(cycle, minDegree));
                        }
                    }
                }
            }
            return(cycle);
        }
        public override string ToString()
        {
            var result = "";

            for (var i = 0; i < AdjacencyMatrix.GetLength(0); i++)
            {
                for (var j = i; j < AdjacencyMatrix.GetLength(1); j++)
                {
                    if (IsEdge(i, j))
                    {
                        if (result.Length > 0)
                        {
                            result += ", ";
                        }
                        result += "<" + i + ", " + j + ">";
                    }
                }
            }
            return(result);
        }
Esempio n. 20
0
        public int[] GetColors()
        {
            Queue <int> toBeVisited = new Queue <int>();
            int         current;

            int[]  colorOfVertices = new int[AdjacencyMatrix.GetLength(0)];
            bool[] visited         = new bool[AdjacencyMatrix.GetLength(0)];

            for (int j = 0; j < visited.Length; j++)
            {
                if (!visited[j])
                {
                    toBeVisited.Enqueue(j);

                    while (toBeVisited.Count != 0)
                    {
                        current          = toBeVisited.Dequeue();
                        visited[current] = true;

                        for (int i = 0; i < CourseNames.Length; i++)
                        {
                            if (AdjacencyMatrix[current, i] == 1)
                            {
                                if (colorOfVertices[i] == colorOfVertices[current])
                                {
                                    colorOfVertices[i] = colorOfVertices[current] + 1;
                                }

                                if (!visited[i])
                                {
                                    toBeVisited.Enqueue(i);
                                }
                            }
                        }
                    }
                }
            }


            return(colorOfVertices);
        }
Esempio n. 21
0
        /// <summary>
        /// Resizes the adjacency matrix after an vertex is added
        /// </summary>
        private void ResizeAdjacencyMatrix()
        {
            if (AdjacencyMatrix != null && AdjacencyMatrix.GetLength(0) != Count)
            {
                int[,] newMatrix = new int[Count, Count];

                for (int x = 0; x < AdjacencyMatrix.GetLength(0); x++)
                {
                    for (int y = 0; y < AdjacencyMatrix.GetLength(0); y++)
                    {
                        newMatrix[x, y] = AdjacencyMatrix[x, y];
                    }
                }

                AdjacencyMatrix = newMatrix;
            }
            else
            {
                AdjacencyMatrix = new int[Count, Count];
            }
        }
Esempio n. 22
0
        public override string ToString()
        {
            string s = "Macierz sąsiedztwa\n  ";
            int    n = AdjacencyMatrix.GetLength(0);

            foreach (var name in Names)
            {
                s += name + " ";
            }
            s += "\n";
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (j == 0)
                    {
                        s += Names[i] + " ";
                    }
                    s += AdjacencyMatrix[i, j] + " ";
                }
                s += "\n";
            }
            s += "\nLista sąsiedztwa\n";

            //s += ArrayHelper.ToString(AdjacencyList, Names);
            s += ArrayHelper.ToString(AdjacencyList);

            s += "\nStopnie \n";
            foreach (var name in Names)
            {
                s += name + " ";
            }
            s += "\n";
            foreach (var degree in Degrees)
            {
                s += degree + " ";
            }
            return(s);
        }
 public bool IsCorrect()
 {
     if (AdjacencyMatrix.GetLength(0) != AdjacencyMatrix.GetLength(1))
     {
         return(false);
     }
     for (var i = 0; i < AdjacencyMatrix.GetLength(0); i++)
     {
         for (var j = i; j < AdjacencyMatrix.GetLength(1); j++)
         {
             if (j == i && AdjacencyMatrix[i, j]) // loops
             {
                 return(false);
             }
             else if (AdjacencyMatrix[i, j] != AdjacencyMatrix[j, i])
             {
                 return(false);
             }
         }
     }
     return(true);
 }
Esempio n. 24
0
        public bool FindC3Naive()
        {
            int n = AdjacencyMatrix.GetLength(0);

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (AdjacencyMatrix[i, j] > 0 && i != j)
                    {
                        for (int k = 0; k < n; k++)
                        {
                            if (k != j && k != i && AdjacencyMatrix[i, k] > 0 && AdjacencyMatrix[j, k] > 0) //k > 0
                            {
                                return(true);
                            }
                        }
                    }
                }
            }
            return(false);
        }
Esempio n. 25
0
        public List <int> JordanCenterHKC()
        {
            int n = AdjacencyMatrix.GetLength(0);

            bool[] visited  = new bool[n];
            int[]  newIndex = new int[n];

            List <int> queue = new List <int>();

            queue.Add(0);
            visited[0] = true;

            int[] C = new int[n];
            C[0] = -1;

            for (int i = 0; i < n; i++)
            {
                foreach (int j in AdjacencyList[i])
                {
                    if (!visited[j])
                    {
                        queue.Add(j);
                        visited[j] = true;
                        C[j]       = i;
                    }
                }
                newIndex[queue[i]] = i;
            }

            int[] tmp = new int[n];
            Array.Copy(C, tmp, n);

            for (int i = 1; i < n; i++)
            {
                C[newIndex[i]] = newIndex[tmp[i]];
            }

            return(HKC(C, newIndex));
        }
Esempio n. 26
0
        public void algFloyda()
        {
            int range = AdjacencyMatrix.GetLength(0);

            for (int a = 0; a < range; a++)
            {
                AdjacencyMatrix[a, a] = 0;
            }
            for (int k = 0; k < range; k++)
            {
                for (int i = 0; i < range; i++)
                {
                    for (int j = 0; j < range; j++)
                    {
                        if (AdjacencyMatrix[i, j] > AdjacencyMatrix[i, k] + AdjacencyMatrix[k, j])
                        {
                            IncidenceMatrix[i, j] = IncidenceMatrix[k, j];
                            AdjacencyMatrix[i, j] = AdjacencyMatrix[i, k] + AdjacencyMatrix[k, j];
                        }
                    }
                }
            }
        }
Esempio n. 27
0
        public List <int> BFS(int startIndex)
        {
            int n = AdjacencyMatrix.GetLength(0);

            if (startIndex < 0 || startIndex >= n)
            {
                throw new ArgumentException("Graf o indexie " + startIndex + " nie istnieje!");
            }

            List <int> queue = new List <int>();

            bool[] visited = new bool[n];

            queue.Add(startIndex);
            visited[startIndex] = true;

            List <int> path = new List <int>();

            while (queue.Count != 0)
            {
                int v = queue[0];
                queue.RemoveAt(0);

                path.Add(v);

                for (int i = 0; i < AdjacencyList[v].Count; i++)
                {
                    if (!visited[AdjacencyList[v][i]])
                    {
                        queue.Add(AdjacencyList[v][i]);
                        visited[AdjacencyList[v][i]] = true;
                    }
                }
            }
            return(path);
        }
Esempio n. 28
0
        public void algPrima()
        {
            int  h1;
            int  nbOfEndVertexOfTheLightestEdge = 0;
            int  startvert = 0;
            bool flag      = false;

            int[,] mstMatrix = new int[AdjacencyMatrix.GetLength(1), AdjacencyMatrix.GetLength(1)];
            bool[] IsVisited = new bool[VertexList.Count];
            IsVisited[0] = true;

            for (int a = 0; a < VertexList.Count - 1; a++)
            {
                for (int b = 0; b < mstMatrix.GetLength(0); b++)
                {
                    if (IsVisited[b])
                    {
                        h1 = getNbEndVertexOfLightestEdge(b, IsVisited);
                        if (h1 == -1)
                        {
                            continue;
                        }
                        if (!flag || AdjacencyMatrix[b, h1] < AdjacencyMatrix[startvert, nbOfEndVertexOfTheLightestEdge])
                        {
                            nbOfEndVertexOfTheLightestEdge = h1;
                            startvert = b;
                            flag      = true;
                        }
                    }
                }
                mstMatrix[startvert, nbOfEndVertexOfTheLightestEdge] = 1;
                IsVisited[nbOfEndVertexOfTheLightestEdge]            = true;
                flag = false;
            }
            endAlgPrima(this, mstMatrix);
        }
Esempio n. 29
0
 public int EdgeCount()
 {
     return(Enumerable.Range(0, AdjacencyMatrix.GetLength(0)).Select(i => VerticeDegree(i)).Sum() / 2);
 }
Esempio n. 30
0
 public bool IsAcyclic()
 {
     return((AdjacencyMatrix.GetLength(0) - 1) == CountEdges());
 }