-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGraph.c
105 lines (91 loc) · 2.16 KB
/
Graph.c
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
// Graph.c ... implementation of Graph ADT
// Written by John Shepherd, May 2013
// Taken and edited from course material
// Taken from course material and adapted to fit my program.
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "Graph.h"
#include "Queue.h"
// graph representation (adjacency matrix)
typedef struct GraphRep {
int nV; // #vertices
int nE; // #edges
int **edges; // matrix of weights (0 == no edge)
} GraphRep;
// check validity of Vertex
int validV (Graph g, Vertex v)
{
return (g != NULL && v >= 0 && v < g->nV);
}
// insert an Edge
// - sets (v,w)
void insertEdge (Graph g, Vertex v, Vertex w, int wt)
{
assert (g != NULL && validV (g, v) && validV (g, w));
// We only need to insert one edge because it is directed.
if (g->edges[v][w] == 0) {
g->edges[v][w] = wt;
g->nE++;
}
}
// remove an Edge
// - unsets (v,w)
void removeEdge (Graph g, Vertex v, Vertex w)
{
assert (g != NULL && validV (g, v) && validV (g, w));
if (g->edges[v][w] != 0) {
g->edges[v][w] = 0;
g->nE--;
}
}
// create an empty graph
Graph newGraph (int nV)
{
assert (nV > 0);
GraphRep *new = malloc (sizeof *new);
assert (new != NULL);
*new = (GraphRep){ .nV = nV, .nE = 0 };
new->edges = calloc ((size_t) nV, sizeof (int *));
assert (new->edges != NULL);
for (int v = 0; v < nV; v++) {
new->edges[v] = calloc ((size_t) nV, sizeof (int));
assert (new->edges[v] != 0);
}
return new;
}
// free memory associated with graph
void dropGraph (Graph g)
{
assert (g != NULL);
for (int v = 0; v < g->nV; v++)
free (g->edges[v]);
free (g->edges);
free (g);
}
// display graph, using names for vertices
void showGraph (Graph g, char **names)
{
assert (g != NULL);
printf ("#vertices=%d, #edges=%d\n\n", g->nV, g->nE);
int v, w;
for (v = 0; v < g->nV; v++) {
printf ("%d %s\n", v, names[v]);
for (w = 0; w < g->nV; w++) {
if (g->edges[v][w]) {
printf ("\t%s (%d)\n", names[w], g->edges[v][w]);
}
}
printf ("\n");
}
}
// True is there is a link between two vertices, false otherwise.
bool isAdjacent(Graph g, Vertex v, Vertex w) {
if (g->edges[v][w]) {
return true;
} else {
return false;
}
}