Skip to content

Commit 3a364d8

Browse files
committed
add 6-10,6-11,6-12,6-13,6-15,6-16,6-17
1 parent 4e23572 commit 3a364d8

7 files changed

+335
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Helper structure to store DFS information for each vertex
2+
typedef struct {
3+
int dfn; // Discovery time
4+
int low; // Lowest vertex reachable
5+
int inStack; // Whether vertex is in stack
6+
} VertexInfo;
7+
8+
// Helper function for Tarjan's algorithm
9+
void Tarjan(Graph G, Vertex v, VertexInfo *info, Vertex *stack, int *top,
10+
int *time, void (*visit)(Vertex), int *visited) {
11+
info[v].dfn = info[v].low = (*time)++;
12+
stack[(*top)++] = v;
13+
info[v].inStack = 1;
14+
15+
PtrToVNode node = G->Array[v];
16+
while (node) {
17+
Vertex w = node->Vert;
18+
if (info[w].dfn == -1) { // Unvisited vertex
19+
Tarjan(G, w, info, stack, top, time, visit, visited);
20+
if (info[w].low < info[v].low)
21+
info[v].low = info[w].low;
22+
} else if (info[w].inStack) { // Back edge to vertex in stack
23+
if (info[w].dfn < info[v].low)
24+
info[v].low = info[w].dfn;
25+
}
26+
node = node->Next;
27+
}
28+
29+
// If v is root of SCC, pop stack and print component
30+
if (info[v].dfn == info[v].low) {
31+
Vertex w;
32+
do {
33+
w = stack[--(*top)];
34+
info[w].inStack = 0;
35+
if (!visited[w]) {
36+
(*visit)(w);
37+
visited[w] = 1;
38+
}
39+
} while (w != v);
40+
printf("\n"); // Print newline after each component
41+
}
42+
}
43+
44+
void StronglyConnectedComponents(Graph G, void (*visit)(Vertex V)) {
45+
// Initialize arrays
46+
VertexInfo *info = (VertexInfo*)malloc(G->NumOfVertices * sizeof(VertexInfo));
47+
Vertex *stack = (Vertex*)malloc(G->NumOfVertices * sizeof(Vertex));
48+
int *visited = (int*)calloc(G->NumOfVertices, sizeof(int));
49+
int top = 0; // Stack top
50+
int time = 0; // DFS timestamp
51+
52+
// Initialize vertex info
53+
for (Vertex v = 0; v < G->NumOfVertices; v++) {
54+
info[v].dfn = -1;
55+
info[v].low = -1;
56+
info[v].inStack = 0;
57+
}
58+
59+
// Run Tarjan's algorithm for each unvisited vertex
60+
for (Vertex v = 0; v < G->NumOfVertices; v++) {
61+
if (info[v].dfn == -1) {
62+
Tarjan(G, v, info, stack, &top, &time, visit, visited);
63+
}
64+
}
65+
66+
// Free memory
67+
free(info);
68+
free(stack);
69+
free(visited);
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
void ShortestDist(LGraph Graph, int dist[], Vertex S) {
2+
// Initialize queue for BFS
3+
int queue[MaxVertexNum];
4+
int front = 0, rear = 0;
5+
bool visited[MaxVertexNum] = {false};
6+
7+
// Initialize all distances to -1 (unreachable)
8+
for (int i = 0; i < Graph->Nv; i++) {
9+
dist[i] = -1;
10+
}
11+
12+
// Set distance to source as 0 and enqueue
13+
dist[S] = 0;
14+
queue[rear++] = S;
15+
visited[S] = true;
16+
17+
// BFS
18+
while (front < rear) {
19+
// Dequeue a vertex
20+
Vertex V = queue[front++];
21+
22+
// Traverse all adjacent vertices
23+
PtrToAdjVNode currentNode = Graph->G[V].FirstEdge;
24+
while (currentNode) {
25+
Vertex W = currentNode->AdjV;
26+
if (!visited[W]) {
27+
// Update distance and enqueue
28+
dist[W] = dist[V] + 1;
29+
queue[rear++] = W;
30+
visited[W] = true;
31+
}
32+
currentNode = currentNode->Next;
33+
}
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
void ShortestDist(MGraph Graph, int dist[], Vertex S) {
2+
bool collected[MaxVertexNum];
3+
int i;
4+
5+
// Initialize
6+
for (i = 0; i < Graph->Nv; i++) {
7+
dist[i] = INFINITY;
8+
collected[i] = false;
9+
}
10+
dist[S] = 0;
11+
12+
while (1) {
13+
// Find the uncollected vertex with smallest distance
14+
Vertex V = -1;
15+
int minDist = INFINITY;
16+
for (i = 0; i < Graph->Nv; i++) {
17+
if (collected[i] == false && dist[i] < minDist) {
18+
V = i;
19+
minDist = dist[i];
20+
}
21+
}
22+
23+
// If no such vertex exists, break
24+
if (V == -1) break;
25+
26+
collected[V] = true;
27+
28+
// Update dist[] through V
29+
for (i = 0; i < Graph->Nv; i++) {
30+
if (collected[i] == false && Graph->G[V][i] > 0) {
31+
if (dist[V] + Graph->G[V][i] < dist[i]) {
32+
dist[i] = dist[V] + Graph->G[V][i];
33+
}
34+
}
35+
}
36+
}
37+
38+
// Convert INFINITY to -1 for unreachable vertices
39+
for (i = 0; i < Graph->Nv; i++) {
40+
if (dist[i] == INFINITY) dist[i] = -1;
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
bool TopSort(LGraph Graph, Vertex TopOrder[]) {
2+
int inDegree[MaxVertexNum] = {0}; // Array to store in-degree of each vertex
3+
int count = 0; // Count of vertices in topological order
4+
Vertex queue[MaxVertexNum]; // Queue to store vertices with 0 in-degree
5+
int front = 0, rear = 0; // Queue pointers
6+
7+
// Calculate in-degree for each vertex
8+
for (int v = 0; v < Graph->Nv; v++) {
9+
PtrToAdjVNode w = Graph->G[v].FirstEdge;
10+
while (w) {
11+
inDegree[w->AdjV]++;
12+
w = w->Next;
13+
}
14+
}
15+
16+
// Initialize queue with vertices that have 0 in-degree
17+
for (int v = 0; v < Graph->Nv; v++) {
18+
if (inDegree[v] == 0) {
19+
queue[rear++] = v;
20+
}
21+
}
22+
23+
// Process vertices in queue
24+
while (front < rear) {
25+
Vertex v = queue[front++];
26+
TopOrder[count++] = v;
27+
28+
// Decrease in-degree for all adjacent vertices
29+
PtrToAdjVNode w = Graph->G[v].FirstEdge;
30+
while (w) {
31+
if (--inDegree[w->AdjV] == 0) {
32+
queue[rear++] = w->AdjV;
33+
}
34+
w = w->Next;
35+
}
36+
}
37+
38+
// If count equals number of vertices, topological sort is possible
39+
return count == Graph->Nv;
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
void merge_pass(ElementType list[], ElementType sorted[], int N, int length) {
2+
int i, j, k, a, b;
3+
int start1, end1, start2, end2;
4+
5+
// Process pairs of sublists
6+
for (i = 0; i < N; i += 2 * length) {
7+
// Define boundaries for first sublist
8+
start1 = i;
9+
end1 = start1 + length < N ? start1 + length : N;
10+
11+
// Define boundaries for second sublist
12+
start2 = end1;
13+
end2 = start2 + length < N ? start2 + length : N;
14+
15+
// Merge the two sublists
16+
k = start1; // Starting position in sorted array
17+
a = start1; // Index for first sublist
18+
b = start2; // Index for second sublist
19+
20+
// Compare and merge elements from both sublists
21+
while (1)
22+
{
23+
if (a == end1 && b == end2) break;
24+
else if (a == end1) sorted[k++] = list[b++];
25+
else if (b == end2) sorted[k++] = list[a++];
26+
else if (list[a] <= list[b]) sorted[k++] = list[a++];
27+
else sorted[k++] = list[b++];
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
void ShortestDist(MGraph Graph, int dist[], int count[], Vertex S) {
2+
int i;
3+
bool collected[MaxVertexNum];
4+
5+
// Initialize
6+
for (i = 0; i < Graph->Nv; i++) {
7+
dist[i] = INFINITY;
8+
count[i] = 0;
9+
collected[i] = false;
10+
}
11+
12+
// Initialize source vertex
13+
dist[S] = 0;
14+
count[S] = 1;
15+
16+
while (1) {
17+
// Find the uncollected vertex with minimum distance
18+
int minV = -1;
19+
int minDist = INFINITY;
20+
for (i = 0; i < Graph->Nv; i++) {
21+
if (!collected[i] && dist[i] < minDist) {
22+
minDist = dist[i];
23+
minV = i;
24+
}
25+
}
26+
27+
// If no such vertex exists, break
28+
if (minV == -1) break;
29+
30+
collected[minV] = true;
31+
// Update dist[] and count[] through minV
32+
for (i = 0; i < Graph->Nv; i++) {
33+
if (!collected[i] && Graph->G[minV][i] < INFINITY) {
34+
if (dist[minV] + Graph->G[minV][i] < dist[i]) {
35+
// Found a shorter path
36+
dist[i] = dist[minV] + Graph->G[minV][i];
37+
count[i] = count[minV];
38+
}
39+
else if (dist[minV] + Graph->G[minV][i] == dist[i]) {
40+
// Found another shortest path
41+
count[i] += count[minV];
42+
}
43+
}
44+
}
45+
}
46+
47+
// Convert unreachable vertices' distance to -1
48+
for (i = 0; i < Graph->Nv; i++) {
49+
if (dist[i] == INFINITY) {
50+
dist[i] = -1;
51+
count[i] = 0;
52+
}
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
void ShortestDist(MGraph Graph, int dist[], int path[], Vertex S) {
2+
bool collected[MaxVertexNum];
3+
int V, W, minV;
4+
WeightType minDist;
5+
6+
// Initialize
7+
for (V = 0; V < Graph->Nv; V++) {
8+
dist[V] = INFINITY;
9+
path[V] = -1;
10+
collected[V] = false;
11+
}
12+
13+
// Set source vertex
14+
dist[S] = 0;
15+
16+
while (1) {
17+
// Find the uncollected vertex with minimum distance
18+
minDist = INFINITY;
19+
minV = -1;
20+
21+
for (V = 0; V < Graph->Nv; V++) {
22+
if (collected[V] == false && dist[V] < minDist) {
23+
minDist = dist[V];
24+
minV = V;
25+
}
26+
}
27+
28+
// If no such vertex exists, break
29+
if (minV == -1) break;
30+
31+
collected[minV] = true;
32+
33+
// Update dist[] and path[] through minV
34+
for (W = 0; W < Graph->Nv; W++) {
35+
if (collected[W] == false && Graph->G[minV][W] < INFINITY) {
36+
if (dist[minV] + Graph->G[minV][W] < dist[W]) {
37+
dist[W] = dist[minV] + Graph->G[minV][W];
38+
path[W] = minV;
39+
} else if (dist[minV] + Graph->G[minV][W] == dist[W] &&
40+
GetPathLength(path, minV, S) + 1 < GetPathLength(path, W, S)) {
41+
// If same distance but fewer edges, update path
42+
path[W] = minV;
43+
}
44+
}
45+
}
46+
}
47+
48+
// Convert unreachable vertices' distances to -1
49+
for (V = 0; V < Graph->Nv; V++) {
50+
if (dist[V] == INFINITY) {
51+
dist[V] = -1;
52+
}
53+
}
54+
}
55+
56+
// Helper function to count the number of edges in a path
57+
int GetPathLength(int path[], int V, int S) {
58+
int count = 0;
59+
while (V != S && V != -1) {
60+
count++;
61+
V = path[V];
62+
}
63+
return V == -1 ? INFINITY : count;
64+
}

0 commit comments

Comments
 (0)