#include
#include
#include
struct node
{
int label;
struct node* next;
};
typedef struct node Node;
struct graph
{
int num_vertices;
Node** adj_list;
};
typedef struct graph Graph;
Node* createNode(int v)
{
Node* newNode = malloc(sizeof(Node));
newNode->label = v;
newNode->next = NULL;
return newNode;
}
Graph* CreateNullGraph(int vertices)
{
Graph* G = malloc(sizeof(Graph));
G->num_vertices = vertices;
G->adj_list = malloc(vertices * sizeof(Node*));
int i;
for (i = 0; i < vertices; i++) {
G->adj_list[i] = NULL;
}
return G;
}
void add_edge(Graph* G, int src, int dest,int directed)
{
Node* newNode = createNode(dest);
newNode->next = G->adj_list[src];
G->adj_list[src] = newNode;
if(!directed)
{
Node* srcNode = createNode(src);
srcNode->next = G->adj_list[dest];
G->adj_list[dest] = srcNode;
}
}
void printGraph(Graph* G)
{
int v;
for (v = 0; v < G->num_vertices; v++)
{
Node* temp = G->adj_list[v];
printf("%d = ", v);
while (temp)
{
printf("%d -> ", temp->label);
temp = temp->next;
}
printf("\n");
}
}
void printDegrees(Graph* G)
{
int v;
for (v = 0; v < G->num_vertices; v++)
{
int d =0;
Node* temp = G->adj_list[v];
printf("degree of node %d = ", v);
while (temp)
{
d++;
temp = temp->next;
}
printf("%d\n",d);
}
}
Graph* CreateFGraph(int m)
{
Graph* G = CreateNullGraph(m);
int i,j;
for(i=0;iadj_list[i];
while (temp)
{
if(temp->label == j)
return 1;
temp = temp->next;
}
}
void AdjMatris(Graph* G)
{
int i,j;
FILE *fp = fopen("output.txt","w");
for(i=0;inum_vertices;i++)
{
for(j=0;jnum_vertices;j++)
{
fprintf(fp,"%d ",isNeighbor(G,i,j));
}
fprintf(fp,"\n");
}
fclose(fp);
}
int main()
{
Graph* G=CreateFGraph(5);
printGraph(G);
printDegrees(G);
AdjMatris(G);
return 0;
}