Skip to content

Instantly share code, notes, and snippets.

@ccjmk
Last active August 29, 2015 13:57
Show Gist options
  • Save ccjmk/9611688 to your computer and use it in GitHub Desktop.
Save ccjmk/9611688 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct vertex
{
int data;
struct edge *edge;
struct vertex *next;
} graphNode;
typedef struct edge
{
int data;
struct edge *next;
} edgeNode;
void createGraph(graphNode* initial);
void displayGraph(graphNode* vertex);
void addEdge(graphNode* vertex);
int **getAdjMatrix(graphNode* vertex, int* matSize);
void showMatrix(int** matrix, int size);
void zeroMatrix(int** matrix, int size);
int getGraphSize( graphNode* vertex);
int main()
{
graphNode *graph1;
int **adjMatrix;
int matSize;
graph1 = (graphNode*)malloc(sizeof(graphNode));
createGraph(graph1);
printf("\n");
displayGraph(graph1);
adjMatrix=getAdjMatrix(graph1, &matSize);
printf("\n\n\n");
showMatrix(adjMatrix, matSize);
return 0;
}
void createGraph(graphNode* initial)
{
int vertexCount=0, i;
graphNode* current = initial;
while(vertexCount==0)
{
printf("Num. of Vertices? ");
scanf("%d", &vertexCount);
//printf("v: %d", vertexCount);
}
for(i=0; i<vertexCount; i++)
{
current->data=i+1;
current->edge = NULL;
addEdge(current);
if(i+1<vertexCount)
{
current->next = (graphNode*)malloc(sizeof(graphNode));
current = current->next;
}
current->next = NULL;
}
return;
}
void displayGraph(graphNode* vertex)
{
edgeNode* current = vertex->edge;
printf("\nVertex: %d", vertex->data);
if(vertex->edge != NULL)
{
while(current!=NULL)
{
printf("\n\t-->%d", current->data);
current=current->next;
}
}
else printf("\n\tVertex has no edges");
if(vertex->next != NULL)
{
displayGraph(vertex->next);
}
else printf("\nLast vertex");
return;
}
void addEdge(graphNode* vertex)
{
edgeNode* current;
int data;
while(data!=-1)
{
printf("\nVertex %d\n\tEdge on which vertex? (-1 to exit)", vertex->data);
scanf("%d", &data);
if(data == -1) break;
if(vertex->edge == NULL)
{
//first edge
vertex->edge=(edgeNode*)malloc(sizeof(edgeNode));
vertex->edge->data = data;
vertex->edge->next = NULL;
}
else
{
//lookup for last edge
current=vertex->edge;
while(current->next!=NULL)
{
current=current->next;
}
current->next=(edgeNode*)malloc(sizeof(edgeNode*));
current->next->data = data;
current->next->next = NULL;
}
}
return;
}
int **getAdjMatrix(graphNode* vertex, int* matSize)
{
int i;
*matSize = getGraphSize(vertex);
int **matrix = (int **)malloc((*matSize) * sizeof(int*));
for( i = 0; i < (*matSize); i++)
{
matrix[i] = (int *)malloc((*matSize) * sizeof(int));
}
zeroMatrix(matrix, *matSize);
while(vertex != NULL)
{
edgeNode* current = vertex->edge;
if(vertex->edge != NULL)
{
while(current!=NULL)
{
//matrix[vertex->data-1][current->data-1]++;
matrix[vertex->data-1][current->data-1]++;
current=current->next;
}
}
vertex = vertex->next;
}
return matrix;
}
void showMatrix(int** matrix, int size)
{
int i,j;
for(i=0; i<size; i++)
{
for(j=0; j<size; j++)
{
printf("%d", matrix[i][j]);
}
printf("\n");
}
}
void zeroMatrix(int** matrix, int size)
{
int i,j;
for(i=0; i<size; i++)
{
for(j=0; j<size; j++)
{
matrix[i][j] = 0;
}
}
}
int getGraphSize( graphNode* vertex)
{
int matSize=0;
while(vertex != NULL)
{
matSize++;
vertex=vertex->next;
}
return matSize;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment