-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgraph.cpp
109 lines (100 loc) · 2.69 KB
/
graph.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <stdio.h>
#include <stdlib.h>
#include "graph.h"
/* Function to create an adjacency list node*/
adjlist_node* createNode(int v) {
adjlist_node *newNode;
newNode = (adjlist_node*) malloc(sizeof(adjlist_node));
if (!newNode) {
printf("Unable to allocate memory for node.\n");
exit(1);
}
newNode->vertex = v;
newNode->next = NULL;
return newNode;
}
/* Function to create a graph with n vertices; Creates both directed and undirected graphs*/
graph* createGraph(int n, graph_type_e type) {
graph *G = (graph*) malloc(sizeof(graph));
if (!G) {
printf("Unable to allocate memory for graph.\n");
exit(1);
}
G->num_vertices = n;
G->type = type;
/* Create an array of adjacency lists*/
G->adjListArr = (adjlist*) malloc(n * sizeof(adjlist));
if (!G->adjListArr) {
printf("Unable to allocate memory for adjacency list array.\n");
exit(1);
}
for (int i = 0; i < n; i++) {
G->adjListArr[i].head = NULL;
G->adjListArr[i].num_members = 0;
}
return G;
}
/*Destroys the graph*/
void destroyGraph(graph *graph) {
if (graph) {
if (graph->adjListArr) {
/*Free up the nodes*/
for (int v = 0; v < graph->num_vertices; v++) {
adjlist_node *adjListPtr = graph->adjListArr[v].head;
while (adjListPtr) {
adjlist_node *tmp = adjListPtr;
adjListPtr = adjListPtr->next;
free(tmp);
}
}
/*Free the adjacency list array*/
free(graph->adjListArr);
}
/*Free the graph*/
free(graph);
}
}
/* Adds an edge to a graph*/
void addEdge(graph *graph, int src, int dest) {
/* Add an edge from src to dst in the adjacency list*/
adjlist_node *newNode = createNode(dest);
newNode->next = graph->adjListArr[src].head;
graph->adjListArr[src].head = newNode;
graph->adjListArr[src].num_members++;
if (graph->type == UNDIRECTED) {
/* Add an edge from dest to src also*/
newNode = createNode(src);
newNode->next = graph->adjListArr[dest].head;
graph->adjListArr[dest].head = newNode;
graph->adjListArr[dest].num_members++;
}
}
/* Function to print the adjacency list of graph*/
void displayGraph(graph *graph) {
for (int i = 0; i < graph->num_vertices; i++) {
adjlist_node *adjListPtr = graph->adjListArr[i].head;
printf("%d: ", i);
while (adjListPtr) {
printf("%d->", adjListPtr->vertex);
adjListPtr = adjListPtr->next;
}
printf("NULL\n");
}
}
/* Return 1 if src is incident of dest */
int isIncident(graph *graph, int src, int dest) {
if (graph->type == UNDIRECTED)
if (graph->adjListArr[src].num_members
> graph->adjListArr[dest].num_members) {
int aux = src;
src = dest;
dest = aux;
}
adjlist_node *adjList = graph->adjListArr[src].head;
while (adjList) {
if (adjList->vertex == dest)
return 1;
adjList = adjList->next;
}
return 0;
}