Exemplo n.º 1
0
        public void AddEdge(int src, int dest)
        {
            AdjNode newNode = new AdjNode(src, dest);

            newNode.next    = glist[src].head;
            glist[src].head = newNode;
        }
Exemplo n.º 2
0
    internal virtual void BellmanFordShortestPath(Graph gph, int source)
    {
        int count = gph.count;

        int[] distance = new int[count];
        int[] path     = new int[count];

        for (int i = 0; i < count; i++)
        {
            distance[i] = int.MaxValue;
        }
        distance[source] = 0;
        for (int i = 0; i < count - 1; i++)
        {
            for (int j = 0; j < count; j++)
            {
                AdjNode head = gph.array[j].head;
                while (head != null)
                {
                    int newDistance = distance[j] + head.cost;
                    if (distance[head.destination] > newDistance)
                    {
                        distance[head.destination] = newDistance;
                        path[head.destination]     = j;
                    }
                    head = head.next;
                }
            }
        }
        for (int i = 0; i < count; i++)
        {
            Console.WriteLine(path[i] + " to " + i + " weight " + distance[i]);
        }
    }
Exemplo n.º 3
0
 public AdjNode(int s, int d)
 {
     src  = s;
     dest = d;
     cost = 1;
     next = null;
 }
Exemplo n.º 4
0
    public virtual void AddEdge(int source, int destination, int cost)
    {
        AdjNode node = new AdjNode(source, destination, cost);

        node.next          = array[source].head;
        array[source].head = node;
    }
Exemplo n.º 5
0
    /* Adds a new node to the beginning of the list */
    internal void addAdj(Vertex _vertex)
    {
        AdjNode newNode = new AdjNode(_vertex);

        newNode.next = head;
        head         = newNode;
    }
Exemplo n.º 6
0
    public virtual void DFSStack(Graph gph)
    {
        int count = gph.count;

        int[]       visited = new int[count];
        int         curr;
        Stack <int> stk = new Stack <int>();

        for (int i = 0; i < count; i++)
        {
            visited[i] = 0;
        }

        visited[0] = 1;
        stk.Push(0);

        while (stk.Count > 0)
        {
            curr = stk.Pop();
            AdjNode head = gph.array[curr].head;
            while (head != null)
            {
                if (visited[head.destination] == 0)
                {
                    visited[head.destination] = 1;
                    stk.Push(head.destination);
                }
                head = head.next;
            }
        }
    }
Exemplo n.º 7
0
    public static void Dijkstra(Graph gph, int source)
    {
        int[] previous = new int[gph.count];
        int[] dist     = new int[gph.count];

        for (int i = 0; i < gph.count; i++)
        {
            previous[i] = -1;
            dist[i]     = int.MaxValue;         //infinite
        }

        dist[source]     = 0;
        previous[source] = -1;

        PriorityQueue <AdjNode> queue = new PriorityQueue <AdjNode>();

        AdjNode node = new AdjNode(source, source, 0);

        queue.Enqueue(node);

        while (queue.Count != 0)
        {
            node = queue.Peek();
            queue.Dequeue();

            AdjList adl = gph.array[node.destination];
            AdjNode adn = adl.head;
            while (adn != null)
            {
                int alt = adn.cost + dist[adn.source];
                if (alt < dist[adn.destination])
                {
                    dist[adn.destination]     = alt;
                    previous[adn.destination] = adn.source;
                    node = new AdjNode(adn.source, adn.destination, alt);
                    queue.Enqueue(node);
                }
                adn = adn.next;
            }
        }

        int count = gph.count;

        for (int i = 0; i < count; i++)
        {
            if (dist[i] == int.MaxValue)
            {
                Console.WriteLine(" node id " + i + "  prev " + previous[i] + " distance : Unreachable");
            }
            else
            {
                Console.WriteLine(" node id " + i + "  prev " + previous[i] + " distance : " + dist[i]);
            }
        }
    }
Exemplo n.º 8
0
    public static void DFSRec(Graph gph, int index, int[] visited)
    {
        AdjNode head = gph.array[index].head;

        while (head != null)
        {
            if (visited[head.destination] == 0)
            {
                visited[head.destination] = 1;
                DFSRec(gph, head.destination, visited);
            }
            head = head.next;
        }
    }
Exemplo n.º 9
0
 public void Print()
 {
     for (int i = 0; i < size; i++)
     {
         AdjNode head = glist[i].head;
         if (head != null)
         {
             while (head != null)
             {
                 Console.WriteLine("vertex v:" + i + "connected to edge:{0}", head.dest);
                 head = head.next;
             }
         }
     }
 }
Exemplo n.º 10
0
    private static void TopologicalSortDFS(Graph gph, int index, int[] visited, Stack <int> stk)
    {
        AdjNode head = gph.array[index].head;

        while (head != null)
        {
            if (visited[head.destination] == 0)
            {
                visited[head.destination] = 1;
                TopologicalSortDFS(gph, head.destination, visited, stk);
            }
            head = head.next;
        }
        stk.Push(index);
    }
Exemplo n.º 11
0
    internal bool isAdjacent(Vertex _vertex)
    {
        bool    pass   = false;    //Turns to true if we determine that two vertices are adjacent
        AdjNode walker = head;

        while (walker != null)
        {
            if (walker.adj == _vertex)
            {
                pass = true;
                break;
            }
            walker = walker.next;
        }

        return(pass);
    }
Exemplo n.º 12
0
        public void prim_MST()
        {
            //(V Log V)+(V Log V) +(V^2)+(V^2 Log V)
            //(E Log V)
            while (Nodes.Count != 0)                                         //V
            {
                Node currentNode = Nodes.Dequeue();                          //O(Log V)
                Visited[currentNode.Vertex] = true;                          //O(1)
                MSTW += N[currentNode.Vertex].Value;                         //O(1)
                mxpQ.Enqueue(N[currentNode.Vertex]);                         //O(Log(V))
                int      count         = 0;                                  //O(1)
                RGBPixel ValueOfVaritx = data.ElementAt(currentNode.Vertex); // O(V)
                foreach (RGBPixel element in data)                           //V
                {
                    N[count].Color = element;                                //O(1)
                    if (currentNode.Vertex != count)                         //O(1)
                    {
                        int x, z, y, res;                                    //O(1)
                        x   = Math.Abs(ValueOfVaritx.red - element.red);     //O(1)
                        y   = Math.Abs(ValueOfVaritx.blue - element.blue);   //O(1)
                        z   = Math.Abs(ValueOfVaritx.green - element.green); //O(1)
                        res = x * x + y * y + z * z;                         //O(1)
                        AdjNode Node1 = new AdjNode(count, Math.Sqrt(res));  //O(1)
                        if (Visited[Node1.Des] == false)                     //O(1)
                        {
                            if (N[Node1.Des].Value > Node1.Weight)           //O(1)
                            {
                                Nodes.ChangeKey(N[Node1.Des], Node1.Weight); //O(Log V)

                                Parent[Node1.Des] = currentNode.Vertex;      //O(1)
                            }
                        }
                    }
                    count++;  //O(1)
                }
            }
            Console.WriteLine("MST is : " + Math.Round(MSTW, 1)); //O(1)

            CreateNewColors(k);                                   //O(K D)
        }
Exemplo n.º 13
0
    public virtual void BFSQueue(Graph gph, int index, int[] visited)
    {
        int         curr;
        Queue <int> que = new Queue <int>();

        visited[index] = 1;
        que.Enqueue(index);

        while (que.Count > 0)
        {
            curr = que.Dequeue();
            AdjNode head = gph.array[curr].head;
            while (head != null)
            {
                if (visited[head.destination] == 0)
                {
                    visited[head.destination] = 1;
                    que.Enqueue(head.destination);
                }
                head = head.next;
            }
        }
    }
Exemplo n.º 14
0
    internal virtual void ShortestPath(Graph gph, int source)     // unweighted graph
    {
        int curr;
        int count = gph.count;

        int[] distance = new int[count];
        int[] path     = new int[count];

        Queue <int> que = new Queue <int>();

        for (int i = 0; i < count; i++)
        {
            distance[i] = -1;
        }
        que.Enqueue(source);
        distance[source] = 0;
        while (que.Count > 0)
        {
            curr = que.Dequeue();
            AdjNode head = gph.array[curr].head;
            while (head != null)
            {
                if (distance[head.destination] == -1)
                {
                    distance[head.destination] = distance[curr] + 1;
                    path[head.destination]     = curr;
                    que.Enqueue(head.destination);
                }
                head = head.next;
            }
        }
        for (int i = 0; i < count; i++)
        {
            Console.WriteLine(path[i] + " to " + i + " weight " + distance[i]);
        }
    }
Exemplo n.º 15
0
    public static void Prims(Graph gph)
    {
        int[] previous = new int[gph.count];
        int[] dist     = new int[gph.count];
        int   source   = 1;

        for (int i = 0; i < gph.count; i++)
        {
            previous[i] = -1;
            dist[i]     = int.MaxValue;
        }

        dist[source]     = 0;
        previous[source] = -1;

        PriorityQueue <AdjNode> queue = new PriorityQueue <AdjNode>();
        AdjNode node = new AdjNode(source, source, 0);

        queue.Enqueue(node);

        while (queue.Count != 0)
        {
            node = queue.Peek();
            queue.Dequeue();

            if (dist[node.destination] < node.cost)
            {
                continue;
            }

            dist[node.destination]     = node.cost;
            previous[node.destination] = node.source;

            AdjList adl = gph.array[node.destination];
            AdjNode adn = adl.head;

            while (adn != null)
            {
                if (previous[adn.destination] == -1)
                {
                    node = new AdjNode(adn.source, adn.destination, adn.cost);
                    queue.Enqueue(node);
                }
                adn = adn.next;
            }
        }

        // Printing result.
        int count = gph.count;

        for (int i = 0; i < count; i++)
        {
            if (dist[i] == int.MaxValue)
            {
                Console.WriteLine(" node id " + i + "  prev " + previous[i] + " distance : Unreachable");
            }
            else
            {
                Console.WriteLine(" node id " + i + "  prev " + previous[i] + " distance : " + dist[i]);
            }
        }
    }