| | |
Topological Sort
![]() |
•
•
Join Date: Jun 2009
Posts: 5
Reputation:
Solved Threads: 0
Will some one please help with the topological sort problem. What am I doing wrong? Thanks in advance for your help!!
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <limits.h> class Vertex { public char label; public Vertex(char lab) { label = lab; } } public class Graphs { private final int MAX_VERTS = 20; private Vertex vertexList[]; // list of vertices private int matrix[][]; // adjacency matrix private int numVerts; // current number of vertices private char sortedArray[]; public Graphs() { vertexList = new Vertex[MAX_VERTS]; matrix = new int[MAX_VERTS][MAX_VERTS]; numVerts = 0; for (int i = 0; i < MAX_VERTS; i++) for (int k = 0; k < MAX_VERTS; k++) matrix[i][k] = 0; sortedArray = new char[MAX_VERTS]; // sorted vert labels } public void addVertex(char lab) { vertexList[numVerts++] = new Vertex(lab); } public void addEdge(int start, int end) { matrix[start][end] = 1; } public void displayVertex(int v) { printf(vertexList[v].label); } // toplogical sort public void topo() { int orig_nVerts = numVerts; //vertices remain, while (numVerts > 0) { // get a vertex with no successors, or -1 int currentVertex = noSuccessors(); if (currentVertex == -1) // must be a cycle { printf("ERROR: Graph has cycles"); return; } // insert vertex label in sorted array (start at end) sortedArray[numVerts - 1] = vertexList[currentVertex].label; deleteVertex(currentVertex); // delete vertex } // vertices all gone; display sortedArray printf("Topologically sorted order: "); for (int j = 0; j < orig_nVerts; j++) printf(sortedArray[j]); printf(""); } public int noSuccessors() // returns vert with no successors (or -1 if no such verts) { boolean isEdge; // edge from row to column in adjMat for (int row = 0; row < numVerts; row++) { isEdge = false; // check edges for (int col = 0; col < numVerts; col++) { if (matrix[row][col] > 0) // if edge to another, { isEdge = true; break; } } if (!isEdge) return row; } return -1; } public void deleteVertex(int delVert) { if (delVert != numVerts - 1) // if not last vertex, delete from vertexList { for (int j = delVert; j < numVerts - 1; j++) vertexList[j] = vertexList[j + 1]; for (int row = delVert; row < numVerts - 1; row++) moveRowUp(row, numVerts); for (int col = delVert; col < numVerts - 1; col++) moveColLeft(col, numVerts - 1); } numVerts--; // one less vertex } private void moveRowUp(int row, int length) { for (int col = 0; col < length; col++) matrix[row][col] = matrix[row + 1][col]; } private void moveColLeft(int col, int length) { for (int row = 0; row < length; row++) matrix[row][col] = matrix[row][col + 1]; } public static void main(String[] args) { Graphs g = new Graphs(); g.addVertex('A'); // 0 g.addVertex('B'); // 1 g.addVertex('C'); // 2 g.addVertex('D'); // 3 g.addVertex('E'); // 4 g.addVertex('F'); // 5 g.addVertex('G'); // 6 g.addVertex('H'); // 7 g.addEdge(0, 3); // AD g.addEdge(0, 4); // AE g.addEdge(1, 4); // BE g.addEdge(2, 5); // CF g.addEdge(3, 6); // DG g.addEdge(4, 6); // EG g.addEdge(5, 7); // FH g.addEdge(6, 7); // GH g.topo(); // do the sort } }
Last edited by WaltP; Oct 20th, 2009 at 1:08 am. Reason: Added CODE tags -- with all the help about them, how could you miss using them????
0
#2 Oct 20th, 2009
•
•
•
•
Will some one please help with the topological sort problem. What am I doing wrong? Thanks in advance for your help!!
2) Not using CODE tags
3) Not explaining what your problem is
4) Not telling us about where in your code the problem is
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
•
•
Join Date: Jun 2009
Posts: 5
Reputation:
Solved Threads: 0
0
#3 Oct 20th, 2009
Thank for commenting!
I had to Write a program to perform a topological sort on a graph. The program should accept a graph (a list of vertices and corresponding edges) as input and produce the topological sorting order as output.
errors:
Line 4: error C2061: syntax error : identifier 'Vertex'
Line 4: error C2059: syntax error : ';'
Line 5: error C2449: found '{' at file scope (missing function header?)
Line 11: error C2059: syntax error : '}'
I had to Write a program to perform a topological sort on a graph. The program should accept a graph (a list of vertices and corresponding edges) as input and produce the topological sorting order as output.
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <limits.h> class Vertex { public char label; public Vertex(char lab) { label = lab; } } public class Graphs { private final int MAX_VERTS = 20; private Vertex vertexList[]; // list of vertices private int matrix[][]; // adjacency matrix private int numVerts; // current number of vertices private char sortedArray[]; public Graphs() { vertexList = new Vertex[MAX_VERTS]; matrix = new int[MAX_VERTS][MAX_VERTS]; numVerts = 0; for (int i = 0; i < MAX_VERTS; i++) for (int k = 0; k < MAX_VERTS; k++) matrix[i][k] = 0; sortedArray = new char[MAX_VERTS]; // sorted vert labels } public void addVertex(char lab) { vertexList[numVerts++] = new Vertex(lab); } public void addEdge(int start, int end) { matrix[start][end] = 1; } public void displayVertex(int v) { printf(vertexList[v].label); } // toplogical sort public void topo() { int orig_nVerts = numVerts; //vertices remain, while (numVerts > 0) { // get a vertex with no successors, or -1 int currentVertex = noSuccessors(); if (currentVertex == -1) // must be a cycle { printf("ERROR: Graph has cycles"); return; } // insert vertex label in sorted array (start at end) sortedArray[numVerts - 1] = vertexList[currentVertex].label; deleteVertex(currentVertex); // delete vertex } // vertices all gone; display sortedArray printf("Topologically sorted order: "); for (int j = 0; j < orig_nVerts; j++) printf(sortedArray[j]); printf(""); } public int noSuccessors() // returns vert with no successors (or -1 if no such verts) { boolean isEdge; // edge from row to column in adjMat for (int row = 0; row < numVerts; row++) { isEdge = false; // check edges for (int col = 0; col < numVerts; col++) { if (matrix[row][col] > 0) // if edge to another, { isEdge = true; break; } } if (!isEdge) return row; } return -1; } public void deleteVertex(int delVert) { if (delVert != numVerts - 1) // if not last vertex, delete from vertexList { for (int j = delVert; j < numVerts - 1; j++) vertexList[j] = vertexList[j + 1]; for (int row = delVert; row < numVerts - 1; row++) moveRowUp(row, numVerts); for (int col = delVert; col < numVerts - 1; col++) moveColLeft(col, numVerts - 1); } numVerts--; // one less vertex } private void moveRowUp(int row, int length) { for (int col = 0; col < length; col++) matrix[row][col] = matrix[row + 1][col]; } private void moveColLeft(int col, int length) { for (int row = 0; row < length; row++) matrix[row][col] = matrix[row][col + 1]; } public static void main(String[] args) { Graphs g = new Graphs(); g.addVertex('A'); // 0 g.addVertex('B'); // 1 g.addVertex('C'); // 2 g.addVertex('D'); // 3 g.addVertex('E'); // 4 g.addVertex('F'); // 5 g.addVertex('G'); // 6 g.addVertex('H'); // 7 g.addEdge(0, 3); // AD g.addEdge(0, 4); // AE g.addEdge(1, 4); // BE g.addEdge(2, 5); // CF g.addEdge(3, 6); // DG g.addEdge(4, 6); // EG g.addEdge(5, 7); // FH g.addEdge(6, 7); // GH g.topo(); // do the sort } }
errors:
Line 4: error C2061: syntax error : identifier 'Vertex'
Line 4: error C2059: syntax error : ';'
Line 5: error C2449: found '{' at file scope (missing function header?)
Line 11: error C2059: syntax error : '}'
![]() |
Similar Threads
- Selection Sort in java (Java)
- Word Frequency Sort Duplication Problem (C++)
- Directed graph (C++)
- Topological Sort using dfsOut method (Java)
- Some help needed with Selection Sort in java, Please!! (Java)
- Need Outlook 2003 addon: sort emails in special folders? (Windows NT / 2000 / XP)
Other Threads in the C Forum
- Previous Thread: I need a program that prompts the user to input a real number and promptly rounds it
- Next Thread: Linear canonical transform
| Thread Tools | Search this Thread |
#include * adobe ansi array asterisks binarysearch centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc database dynamic execv feet fgets file floatingpointvalidation fork function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide inches include incrementoperators infiniteloop input interest intmain() iso kernel keyboard kilometer license linked linkedlist linux list locate looping lowest match matrix meter microsoft number oddnumber opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprograming socketprogramming standard strchr string suggestions systemcall test threads turboc unix urboc user variable voidmain() wab whythiscodecausesegmentationfault windowsapi






