Sample Solution
Solutions are a work in progress.
bfsTree()
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 | Graph bfsTree(Graph g, int src) {
int *pred = malloc(g->nV * sizeof(int));
assert(pred != NULL);
Queue q = QueueNew();
for (int v = 0; v < g->nV; v++) {
pred[v] = -1;
}
pred[src] = src;
QueueEnqueue(q, src);
while (!QueueIsEmpty(q)) {
int u = QueueDequeue(q);
for (int v = 0; v < g->nV; v++) {
if (g->edges[u][v] && pred[v] == -1) {
pred[v] = u;
QueueEnqueue(q, v);
}
}
}
Graph t = GraphNew(g->nV);
for (int v = 0; v < g->nV; v++) {
if (v != src && pred[v] != -1) {
GraphInsertEdge(t, v, pred[v]);
}
}
free(pred);
QueueFree(q);
return t;
}
|
dfsTree()
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 | Graph dfsTree(Graph g, int src) {
int *pred = malloc(g->nV * sizeof(int));
assert(pred != NULL);
for (int v = 0; v < g->nV; v++) {
pred[v] = -1;
}
pred[src] = src;
doDfsTree(g, src, pred);
Graph t = GraphNew(g->nV);
for (int v = 0; v < g->nV; v++) {
if (v != src && pred[v] != -1) {
GraphInsertEdge(t, v, pred[v]);
}
}
free(pred);
return t;
}
static void doDfsTree(Graph g, int u, int *pred) {
for (struct adjNode *curr = g->edges[u]; curr != NULL; curr = curr->next) {
int v = curr->v;
if (pred[v] == -1) {
pred[v] = u;
doDfsTree(g, v, pred);
}
}
}
|
shortestPath()
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 | int shortestPath(Graph g, int src, int dest, int path[]) {
int *pred = malloc(g->nV * sizeof(int));
assert(pred != NULL);
Queue q = QueueNew();
for (int v = 0; v < g->nV; v++) {
pred[v] = -1;
}
pred[src] = src;
QueueEnqueue(q, src);
while (!QueueIsEmpty(q)) {
int u = QueueDequeue(q);
struct adjNode *curr;
for (curr = g->edges[u]; curr != NULL; curr = curr->next) {
int v = curr->v;
if (pred[v] == -1) {
pred[v] = u;
QueueEnqueue(q, v);
}
}
}
int pathLength = 0;
if (pred[dest] != -1) {
int curr = dest;
while (curr != src) {
path[pathLength++] = curr;
curr = pred[curr];
}
path[pathLength++] = src;
for (int i = 0, j = pathLength - 1; i < j; i++, j--) {
int tmp = path[i];
path[i] = path[j];
path[j] = tmp;
}
}
free(pred);
QueueFree(q);
return pathLength;
}
|
Worst-case time complexity: \( O(V + E) \)
cycle()
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 | int cycle(Graph g, int path[]) {
int *pred = malloc(g->nV * sizeof(int));
assert(pred != NULL);
for (int v = 0; v < g->nV; v++) {
pred[v] = -1;
}
for (int v = 0; v < g->nV; v++) {
if (pred[v] == -1) {
pred[v] = v;
int pathLen = doCycle(g, v, pred, path);
if (pathLen != 0) {
free(pred);
return pathLen;
}
}
}
free(pred);
return 0;
}
static int doCycle(Graph g, int u, int *pred, int path[]) {
for (struct adjNode *curr = g->edges[u]; curr != NULL; curr = curr->next) {
int v = curr->v;
if (v == pred[u]) continue;
if (pred[v] != -1) {
int pathLen = 0;
path[pathLen++] = v;
int curr = u;
while (curr != v) {
path[pathLen++] = curr;
curr = pred[curr];
}
path[pathLen++] = v;
return pathLen;
}
pred[v] = u;
int pathLen = doCycle(g, v, pred, path);
if (pathLen != 0) {
return pathLen;
}
}
return 0;
}
|
Worst-case time complexity: \( O(V^2) \)
furthestVertex()
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 | struct result furthestVertex(Graph g, int src) {
int *pred = malloc(g->nV * sizeof(int));
assert(pred != NULL);
Queue q = QueueNew();
for (int v = 0; v < g->nV; v++) {
pred[v] = -1;
}
pred[src] = src;
QueueEnqueue(q, src);
int furthest = src;
while (!QueueIsEmpty(q)) {
int u = QueueDequeue(q);
furthest = u;
for (int v = 0; v < g->nV; v++) {
if (!g->edges[u][v]) continue;
if (pred[v] == -1) {
pred[v] = u;
QueueEnqueue(q, v);
}
}
}
int dist = 0;
int curr = furthest;
while (curr != src) {
dist++;
curr = pred[curr];
}
free(pred);
QueueFree(q);
struct result result = {furthest, dist};
return result;
}
|
largestComponent()
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 | int largestComponent(Graph g, int component[]) {
int *componentNos = malloc(g->nV * sizeof(int));
assert(componentNos != NULL);
for (int v = 0; v < g->nV; v++) {
componentNos[v] = -1;
}
int maxCompSize = 0;
int maxCompNo = 0;
int compNo = 0;
for (int v = 0; v < g->nV; v++) {
if (componentNos[v] == -1) {
int compSize = dfs(g, v, componentNos, compNo);
if (compSize > maxCompSize) {
maxCompSize = compSize;
maxCompNo = compNo;
}
compNo++;
}
}
int compSize = 0;
for (int v = 0; v < g->nV; v++) {
if (componentNos[v] == maxCompNo) {
component[compSize++] = v;
}
}
free(componentNos);
return compSize;
}
static int dfs(Graph g, int u, int componentNos[], int compNo) {
int compSize = 1;
componentNos[u] = compNo;
struct adjNode *curr;
for (curr = g->edges[u]; curr != NULL; curr = curr->next) {
int v = curr->v;
if (componentNos[v] == -1) {
compSize += dfs(g, v, componentNos, compNo);
}
}
return compSize;
}
|