Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
7 views
3 pages
TOPSORT
Uploaded by
vijaykrishna2k24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download
Save
Save TOPSORT For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
0 ratings
0% found this document useful (0 votes)
7 views
3 pages
TOPSORT
Uploaded by
vijaykrishna2k24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Carousel Previous
Carousel Next
Download
Save
Save TOPSORT For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
Download
Save TOPSORT For Later
You are on page 1
/ 3
Search
Fullscreen
#include <stdio.
h>
#define MAX 10
int i;
// Adjacency matrix representing the graph
int a[MAX][MAX] = { // 0 1 2 3 4 5 6
{0, 1, 1, 0, 0, 0, 0}, // 0
{0, 0, 1, 0, 0, 1, 0}, // 1
{0, 0, 0, 1, 0, 0, 0}, // 2
{0, 0, 0, 0, 0, 0, 0}, // 3
{0, 0, 0, 0, 0, 0, 0}, // 4
{0, 0, 0, 1, 1, 0, 0}, // 5
{0, 1, 0, 0, 0, 1, 0} // 6
};
int stack[30], que[30], vis[30];
int top = -1, fr = -1, rr = -1, n = 7;
// Function to add an element to the queue
void add(int x) {
if (rr < 0) fr++;
rr++;
que[rr] = x;
}
// Function to push an element onto the stack
void push(int x) {
stack[++top] = x;
}
// Function to pop an element from the stack
int pop() {
return stack[top--];
}
// Depth-First Search (DFS) function
void dfs(int u) {
vis[u] = 1;
for (int i = 0; i < n; i++) {
if (vis[i] == 0 && a[u][i] != 0) {
dfs(i);
}
}
push(u);
}
// Topological Sort function
void topsort() {
int i;
top = -1;
rr = -1;
for (i = 0; i < n; i++)
vis[i] = 0;
for (i = 0; i < n; i++) {
if (vis[i] == 0)
dfs(i);
}
while (top >= 0) {
add(pop());
}
}
int main() {
int seq[10][MAX];
topsort();
printf("Topological Sort Order:\n");
for (int i = 0; i < n; i++) {
seq[0][i] = que[i];
printf("%6d", que[i]);
}
printf("\n");
return 0;
}
You might also like
Task 10 To 14
PDF
No ratings yet
Task 10 To 14
17 pages
4 - Topological Sort
PDF
No ratings yet
4 - Topological Sort
2 pages
Qef G
PDF
No ratings yet
Qef G
9 pages
Adalab
PDF
No ratings yet
Adalab
11 pages
Graph Bfs Dfs
PDF
No ratings yet
Graph Bfs Dfs
3 pages
Experiment 6
PDF
No ratings yet
Experiment 6
5 pages
12) Create A Graph and Represent It Using Adjacency Matrix .Implement Using BFS and DFS Traversals
PDF
No ratings yet
12) Create A Graph and Represent It Using Adjacency Matrix .Implement Using BFS and DFS Traversals
4 pages
LAB5 Topological
PDF
No ratings yet
LAB5 Topological
2 pages
Hard C Programs
PDF
No ratings yet
Hard C Programs
10 pages
Tabij
PDF
No ratings yet
Tabij
16 pages
Satveer Da A
PDF
No ratings yet
Satveer Da A
30 pages
Wa0002
PDF
No ratings yet
Wa0002
14 pages
Assembly Line Scheduling and Optimal Path
PDF
No ratings yet
Assembly Line Scheduling and Optimal Path
57 pages
5
PDF
No ratings yet
5
2 pages
Ds Exp 9
PDF
No ratings yet
Ds Exp 9
9 pages
Topological
PDF
No ratings yet
Topological
2 pages
DAA Assignment
PDF
No ratings yet
DAA Assignment
20 pages
0th Element of Arrays Left Uninitialized Only 19 Useable: Depth First Search
PDF
No ratings yet
0th Element of Arrays Left Uninitialized Only 19 Useable: Depth First Search
8 pages
Ada Lab Bcsl404 Manual Ise - Hkbkce - Corrected
PDF
No ratings yet
Ada Lab Bcsl404 Manual Ise - Hkbkce - Corrected
28 pages
IRP
PDF
No ratings yet
IRP
15 pages
Topological Sorting
PDF
No ratings yet
Topological Sorting
3 pages
Practical 3 & 4
PDF
No ratings yet
Practical 3 & 4
6 pages
Aoa
PDF
No ratings yet
Aoa
16 pages
CN Lab Manual
PDF
No ratings yet
CN Lab Manual
55 pages
Breadth First Search
PDF
No ratings yet
Breadth First Search
2 pages
Samsung
PDF
No ratings yet
Samsung
58 pages
New Text Document
PDF
No ratings yet
New Text Document
4 pages
DFS
PDF
No ratings yet
DFS
3 pages
DAA Exp-5
PDF
No ratings yet
DAA Exp-5
4 pages
DAA Term Work After Mid Term
PDF
No ratings yet
DAA Term Work After Mid Term
13 pages
IC-Assignment 04 128
PDF
No ratings yet
IC-Assignment 04 128
6 pages
Topological Order
PDF
No ratings yet
Topological Order
2 pages
Design and Analysis of Algorithms - 20ISL57A Program 3 - Implement and Analyze Topological Sorting in A Given Directed Graph
PDF
No ratings yet
Design and Analysis of Algorithms - 20ISL57A Program 3 - Implement and Analyze Topological Sorting in A Given Directed Graph
2 pages
Algorithm
PDF
No ratings yet
Algorithm
33 pages
AOA
PDF
No ratings yet
AOA
12 pages
Ada Lab Programs
PDF
No ratings yet
Ada Lab Programs
18 pages
A5-Set-A-aTopological Sort
PDF
No ratings yet
A5-Set-A-aTopological Sort
11 pages
DFS Using STACK and BFS Using Queue
PDF
No ratings yet
DFS Using STACK and BFS Using Queue
4 pages
Maximum Subarray Sum Using Divide and Conquer Approach: Code: - (Without Array Elements Print)
PDF
No ratings yet
Maximum Subarray Sum Using Divide and Conquer Approach: Code: - (Without Array Elements Print)
49 pages
Graph 1
PDF
No ratings yet
Graph 1
64 pages
DAA Exp - 3.1
PDF
No ratings yet
DAA Exp - 3.1
7 pages
BFS DFS and BICONNECTED Programs
PDF
No ratings yet
BFS DFS and BICONNECTED Programs
13 pages
Daa 2
PDF
No ratings yet
Daa 2
9 pages
Depth First Search
PDF
No ratings yet
Depth First Search
2 pages
Untitled 1: 1. Degree of Vertices
PDF
No ratings yet
Untitled 1: 1. Degree of Vertices
12 pages
Discrete Math Practical Student - 05!04!2021
PDF
No ratings yet
Discrete Math Practical Student - 05!04!2021
11 pages
Ada Lab Manual
PDF
No ratings yet
Ada Lab Manual
31 pages
Design and Analysis of Algorithms Lab
PDF
No ratings yet
Design and Analysis of Algorithms Lab
11 pages
Ads 1
PDF
No ratings yet
Ads 1
24 pages
ADA Lab Topological Sort Program 5
PDF
No ratings yet
ADA Lab Topological Sort Program 5
2 pages
Program-1 Write A Program in C To Create Two Sets and Perform The Union Operation On Sets
PDF
No ratings yet
Program-1 Write A Program in C To Create Two Sets and Perform The Union Operation On Sets
22 pages
Mastering in Dsa 1
PDF
No ratings yet
Mastering in Dsa 1
13 pages
Ads Lab Manual (Micro)
PDF
No ratings yet
Ads Lab Manual (Micro)
17 pages
AA Record - 231011 - 165258
PDF
No ratings yet
AA Record - 231011 - 165258
21 pages
Sds
PDF
No ratings yet
Sds
22 pages
CN Lab Part B
PDF
No ratings yet
CN Lab Part B
23 pages
Dsaoe Exp6b Harsh 034
PDF
No ratings yet
Dsaoe Exp6b Harsh 034
8 pages
CN All Programs
PDF
No ratings yet
CN All Programs
25 pages
Program 1: Code and Analyze Solutions To Following Problem With Given Strategies Ii. Knap Sack Using Dynamic Approach
PDF
No ratings yet
Program 1: Code and Analyze Solutions To Following Problem With Given Strategies Ii. Knap Sack Using Dynamic Approach
26 pages
No Ph.D. Game Design With Three.js
From Everand
No Ph.D. Game Design With Three.js
Nikiforos Kontopoulos
No ratings yet