Skip to main content

Q1

 // Dijkstra's Algorithm in C


#include <stdio.h>

#define INFINITY 9999

#define MAX 10


void Dijkstra(int Graph[MAX][MAX], int n, int start);


void Dijkstra(int Graph[MAX][MAX], int n, int start) {

  int cost[MAX][MAX], distance[MAX], pred[MAX];

  int visited[MAX], count, mindistance, nextnode, i, j;


  // Creating cost matrix

  for (i = 0; i < n; i++)

    for (j = 0; j < n; j++)

      if (Graph[i][j] == 0)

        cost[i][j] = INFINITY;

      else

        cost[i][j] = Graph[i][j];


  for (i = 0; i < n; i++) {

    distance[i] = cost[start][i];

    pred[i] = start;

    visited[i] = 0;

  }


  distance[start] = 0;

  visited[start] = 1;

  count = 1;


  while (count < n - 1) {

    mindistance = INFINITY;


    for (i = 0; i < n; i++)

      if (distance[i] < mindistance && !visited[i]) {

        mindistance = distance[i];

        nextnode = i;

      }


    visited[nextnode] = 1;

    for (i = 0; i < n; i++)

      if (!visited[i])

        if (mindistance + cost[nextnode][i] < distance[i]) {

          distance[i] = mindistance + cost[nextnode][i];

          pred[i] = nextnode;

        }

    count++;

  }


  // Printing the distance

  for (i = 0; i < n; i++)

    if (i != start) {

      printf("\nDistance from source to %d: %d", i, distance[i]);

    }

}

int main() {

  int Graph[MAX][MAX], i, j, n, u;

  n = 5;


  Graph[0][0] = 0;

  Graph[0][1] = 6;

  Graph[0][2] = 1;

  Graph[0][3] = 2;

  Graph[0][4] = 0;



  Graph[1][0] = 6;

  Graph[1][1] = 0;

  Graph[1][2] = 2;

  Graph[1][3] = 0;

  Graph[1][4] = 0;



  Graph[2][0] = 1;

  Graph[2][1] = 2;

  Graph[2][2] = 0;

  Graph[2][3] = 1;

  Graph[2][4] = 3;



  Graph[3][0] = 2;

  Graph[3][1] = 0;

  Graph[3][2] = 1;

  Graph[3][3] = 0;

  Graph[3][4] = 0;



  Graph[4][0] = 0;

  Graph[4][1] = 0;

  Graph[4][2] = 3;

  Graph[4][3] = 0;

  Graph[4][4] = 0;




  u = 0;

  Dijkstra(Graph, n, u);


  return 0;

}


Comments

Popular posts from this blog

IELTS Spoken Class Adminssion Scenario - 01

.......  Student: Hello, May I come in, sir ? Optional (student): May I sit ? Sir:  Please have a seat. Sir: How may I help you, Sir ? Student: I would like to admit in your spoken course. Sir: Oh sure. Student: How many days are there in a week ? Sir: There are three classes in a week. Student: What time do you offer class ? Sir: We have class at 11am / 4pm / 6pm / 8pm

Php Learning Time

 Differences of explode( ) and implode( ) in php: explode: একটা স্ট্রিংকে কোনো একটা সেপারেটরের বেসিসে অ্যারেতে কনভার্ট করে, যেমন  <?php $text="Hello How are you?"; print_r(explode(" ",$text)); ?> This will give output of  Array (     [0] => Hello     [1] => How     [2] => are     [3] => you? ) Differences of array_splice( ) and array_slice( ) ধরেন আসল অ্যারে হচ্ছে    $arr =[ "Hello" , "this" , "is" , "test" , "text" ];    এখন এটাকে স্লাইসিং করার জন্য আমরা উপরের দুইটা মেথড ই ব্যবহার করতে পারি , কিন্তু array_splice এটা ইউজ করলে অরজিনাল array ও চেঞ্জ হয়ে যাবে, মানে যদি আমরা এভাবে লিখি  $var2 = array_splice ( $arr , 0 , 2 );   তাইলে $var2 এর ভিতরে থাকবে ["Hello" , "this"] ,  আর অরজিনাল array তে বাকি থাকবে ["is", "test", "text"] কিন্তু যদি আমরা ইউজ করি তাহলে অরজিনাল array আগের মতোই থাকবে পাশাপাশি $var2 এর মধ্যে ভ্যালু গুলা এসে পড়বে  Array (     [0] => ...