code.c
unknown
c_cpp
a year ago
3.4 kB
9
Indexable
/* Coding Assignment 5 */ /* Name: Nadia Thonu */ /* id: 1001983393 */ #include <stdlib.h> #include <stdio.h> #include <string.h> #include <time.h> #define MAX 50 typedef struct { char label[6]; int distance; int previous; int visited; } Vertex; void addVertex(char *label, Vertex VertexArray[], int *VertexCount) { strcpy(VertexArray[(*VertexCount)].label,label); VertexArray[(*VertexCount)].distance = -1; VertexArray[(*VertexCount)].previous = -1; VertexArray[(*VertexCount)].visited = 0; (*VertexCount)++; } void addEdge(int start, int end,int weight, int AdjMatrix[][MAX]) { AdjMatrix[start][end] = weight; } void Dijkstra(int AdjMatrix[][MAX],Vertex VertexArray[],int VertexCount) { for (x = 0; x < VertexCount-1; x++) { for(i = 0; i < VertexCount; i++) { relaxing the edges } int SmallestVertexIndex = -1; int SmallestVertex = INT_MAX; for(i = 0; i < VertexCount; i++) { if(VertexArray[i].distance!=-1 && VertexArray[i].distance<SmallestVertex) { SmallestVertex=VertexArray[i].distance; SmallestVertexIndex=i; } } } } void CreateAdjacencyMatrix(char *argv[],int AdjMatrix[][MAX],Vertex VertexArray[]) { int start = 0, end = -1; int i = 0, j = 0; char buffer[100] = {}; FILE *file = fopen(argv[1],"r"); if(!file) { printf("Exiting : No such file or directory\n"); exit(0); } /* initialize adjacency matrix to -1 */ for(i = 0; i < MAX; i++) for(j = 0; j < MAX; j++) AdjMatrix[i][j] = -1; //starting read form file char *token = NULL; int VertexCount=0; while(fgets(buffer,sizeof(buffer)-1,file)) { if (buffer[strlen(buffer)-1] == '\n') buffer[strlen(buffer)-1] = '\0'; token = strtok(buffer, ","); //printf("%s ", token); //start=VertexCount; addVertex(token, VertexArray, &VertexCount); end=-1; while (token != NULL) { // printf("%s ", token); token = strtok(NULL, ","); // printf("%s ", token); if(end==-1) { end=atoi(token); } else { int weight=atoi(token); printf("%d %d %d\n",start,end,weight); addEdge(start, end,weight, AdjMatrix); end=-1; } } start++; //printf("\n"); } fclose(file); printf("\n"); for(i=0; i<VertexCount; i++) { for(j=0; j<VertexCount; j++) { printf("%5d\t",AdjMatrix[i][j]); } printf("\n"); } printf("\n"); printf("\n\nI\tL\tD\tP\tV\n"); for(i=0; i<VertexCount; i++) { printf("%d\t%s\t%d\t%d\t%d\n",i,VertexArray[i].label,VertexArray[i].distance,VertexArray[i].previous,VertexArray[i].visited); } printf("\n"); } int main(int argc, char * argv[]) { int AdjMatrix[MAX][MAX]; Vertex VertexArray[MAX]; argv[0]="abcd"; argv[1]="Graph.txt"; CreateAdjacencyMatrix(argv,AdjMatrix,VertexArray); return 0; }
Editor is loading...
Leave a Comment