Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define MAX 5
  5.  
  6. int dijkstra(int *nodes[MAX],int initialNode,int destinationNode,int *weightMatrix[MAX][MAX]);
  7.  
  8.  
  9. struct link{
  10.     int targetNode_id;
  11.     int weight;
  12. };
  13.  
  14. struct node{
  15.     int id;
  16.     link links[4];
  17. };
  18.  
  19. int main(){
  20.     //initialize variables
  21.     int i,j;
  22.     int weightsMatrix[MAX][MAX] = {{0,100,800,0,0},{0,0,0,300,600},{0,200,0,500,0},{0,0,0,0,0},{0,0,0,200,0}};
  23.     node initialNode;
  24.     node nodes[MAX];
  25.     initialNode = nodes[0];
  26.    
  27.     //assigning nodes ids
  28.     for(i = 0; i < MAX; i++){
  29.         nodes[i].id = i;
  30.     }
  31.  
  32.     for(int nodeIndex = 0; nodeIndex < MAX; nodeIndex++){
  33.         int linkCount = 0;
  34.         node nodeInstance = node();
  35.         nodeInstance.id = nodeIndex;
  36.         //printf("Node Instance ID: %d\n ",nodeInstance.id);
  37.         for(int weightIndex = 0; weightIndex < MAX; weightIndex++){
  38.             if(0 != weightsMatrix[nodeIndex][weightIndex])
  39.             {
  40.                 nodeInstance.links[linkCount].targetNode_id = weightIndex;
  41.                 //printf("\tLink index: %d\n\t\tSource: %d\n\t\tTarget: %d\n\t\tWeight: %d\n\n\n", linkCount, nodeInstance.id, weightIndex, weightsMatrix[nodeIndex][weightIndex]);
  42.                 nodeInstance.links[linkCount].weight = weightsMatrix[nodeIndex][weightIndex];
  43.                 linkCount++;
  44.             }
  45.         }
  46.         nodes[nodeIndex] = nodeInstance;
  47.     }
  48. }
  49.  
  50. int dijkstra(node *nodes[MAX],node initialNode,node destinationNode,int *weightMatrix[MAX][MAX]){
  51.     initialNode = nodes[0]->id;
  52.     destinationNode = nodes[4]->id;
  53.    
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement