944,100 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3243
  • C RSS
Oct 20th, 2009
0

Topological Sort

Expand Post »
Will some one please help with the topological sort problem. What am I doing wrong? Thanks in advance for your help!!
  1. #include <stdio.h>
  2. #include <limits.h>
  3.  
  4. class Vertex
  5. {
  6. public char label;
  7. public Vertex(char lab)
  8. {
  9. label = lab;
  10. }
  11. }
  12.  
  13. public class Graphs
  14. {
  15. private final int MAX_VERTS = 20;
  16. private Vertex vertexList[]; // list of vertices
  17. private int matrix[][]; // adjacency matrix
  18. private int numVerts; // current number of vertices
  19. private char sortedArray[];
  20. public Graphs()
  21. {
  22. vertexList = new Vertex[MAX_VERTS];
  23. matrix = new int[MAX_VERTS][MAX_VERTS];
  24. numVerts = 0;
  25. for (int i = 0; i < MAX_VERTS; i++)
  26. for (int k = 0; k < MAX_VERTS; k++)
  27. matrix[i][k] = 0;
  28. sortedArray = new char[MAX_VERTS]; // sorted vert labels
  29. }
  30.  
  31. public void addVertex(char lab)
  32. {
  33. vertexList[numVerts++] = new Vertex(lab);
  34. }
  35.  
  36. public void addEdge(int start, int end)
  37. {
  38. matrix[start][end] = 1;
  39. }
  40.  
  41. public void displayVertex(int v)
  42. {
  43. printf(vertexList[v].label);
  44. }
  45.  
  46. // toplogical sort
  47. public void topo()
  48. {
  49. int orig_nVerts = numVerts;
  50.  
  51. //vertices remain,
  52. while (numVerts > 0)
  53. {
  54. // get a vertex with no successors, or -1
  55. int currentVertex = noSuccessors();
  56. if (currentVertex == -1) // must be a cycle
  57. {
  58. printf("ERROR: Graph has cycles");
  59. return;
  60. }
  61. // insert vertex label in sorted array (start at end)
  62. sortedArray[numVerts - 1] = vertexList[currentVertex].label;
  63.  
  64. deleteVertex(currentVertex); // delete vertex
  65. }
  66.  
  67. // vertices all gone; display sortedArray
  68. printf("Topologically sorted order: ");
  69. for (int j = 0; j < orig_nVerts; j++)
  70. printf(sortedArray[j]);
  71. printf("");
  72. }
  73.  
  74. public int noSuccessors() // returns vert with no successors (or -1 if no such verts)
  75. {
  76. boolean isEdge; // edge from row to column in adjMat
  77.  
  78. for (int row = 0; row < numVerts; row++) {
  79. isEdge = false; // check edges
  80. for (int col = 0; col < numVerts; col++) {
  81. if (matrix[row][col] > 0) // if edge to another,
  82. {
  83. isEdge = true;
  84. break;
  85. }
  86. }
  87. if (!isEdge)
  88. return row;
  89. }
  90. return -1;
  91. }
  92.  
  93. public void deleteVertex(int delVert) {
  94. if (delVert != numVerts - 1) // if not last vertex, delete from vertexList
  95. {
  96. for (int j = delVert; j < numVerts - 1; j++)
  97. vertexList[j] = vertexList[j + 1];
  98.  
  99. for (int row = delVert; row < numVerts - 1; row++)
  100. moveRowUp(row, numVerts);
  101.  
  102. for (int col = delVert; col < numVerts - 1; col++)
  103. moveColLeft(col, numVerts - 1);
  104. }
  105. numVerts--; // one less vertex
  106. }
  107.  
  108. private void moveRowUp(int row, int length) {
  109. for (int col = 0; col < length; col++)
  110. matrix[row][col] = matrix[row + 1][col];
  111. }
  112.  
  113. private void moveColLeft(int col, int length) {
  114. for (int row = 0; row < length; row++)
  115. matrix[row][col] = matrix[row][col + 1];
  116. }
  117.  
  118. public static void main(String[] args) {
  119. Graphs g = new Graphs();
  120. g.addVertex('A'); // 0
  121. g.addVertex('B'); // 1
  122. g.addVertex('C'); // 2
  123. g.addVertex('D'); // 3
  124. g.addVertex('E'); // 4
  125. g.addVertex('F'); // 5
  126. g.addVertex('G'); // 6
  127. g.addVertex('H'); // 7
  128.  
  129. g.addEdge(0, 3); // AD
  130. g.addEdge(0, 4); // AE
  131. g.addEdge(1, 4); // BE
  132. g.addEdge(2, 5); // CF
  133. g.addEdge(3, 6); // DG
  134. g.addEdge(4, 6); // EG
  135. g.addEdge(5, 7); // FH
  136. g.addEdge(6, 7); // GH
  137.  
  138. g.topo(); // do the sort
  139. }
  140. }
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????
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
largedimples is offline Offline
10 posts
since Jun 2009
Oct 20th, 2009
0
Re: Topological Sort
Will some one please help with the topological sort problem. What am I doing wrong? Thanks in advance for your help!!
1) Using a useless BOLD for your question
2) Not using CODE tags
3) Not explaining what your problem is
4) Not telling us about where in your code the problem is
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Oct 20th, 2009
0
Re: Topological Sort
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.

  1. #include <stdio.h>
  2. #include <limits.h>
  3.  
  4. class Vertex
  5. {
  6. public char label;
  7. public Vertex(char lab)
  8. {
  9. label = lab;
  10. }
  11. }
  12.  
  13. public class Graphs
  14. {
  15. private final int MAX_VERTS = 20;
  16. private Vertex vertexList[]; // list of vertices
  17. private int matrix[][]; // adjacency matrix
  18. private int numVerts; // current number of vertices
  19. private char sortedArray[];
  20. public Graphs()
  21. {
  22. vertexList = new Vertex[MAX_VERTS];
  23. matrix = new int[MAX_VERTS][MAX_VERTS];
  24. numVerts = 0;
  25. for (int i = 0; i < MAX_VERTS; i++)
  26. for (int k = 0; k < MAX_VERTS; k++)
  27. matrix[i][k] = 0;
  28. sortedArray = new char[MAX_VERTS]; // sorted vert labels
  29. }
  30.  
  31. public void addVertex(char lab)
  32. {
  33. vertexList[numVerts++] = new Vertex(lab);
  34. }
  35.  
  36. public void addEdge(int start, int end)
  37. {
  38. matrix[start][end] = 1;
  39. }
  40.  
  41. public void displayVertex(int v)
  42. {
  43. printf(vertexList[v].label);
  44. }
  45.  
  46. // toplogical sort
  47. public void topo()
  48. {
  49. int orig_nVerts = numVerts;
  50.  
  51. //vertices remain,
  52. while (numVerts > 0)
  53. {
  54. // get a vertex with no successors, or -1
  55. int currentVertex = noSuccessors();
  56. if (currentVertex == -1) // must be a cycle
  57. {
  58. printf("ERROR: Graph has cycles");
  59. return;
  60. }
  61. // insert vertex label in sorted array (start at end)
  62. sortedArray[numVerts - 1] = vertexList[currentVertex].label;
  63.  
  64. deleteVertex(currentVertex); // delete vertex
  65. }
  66.  
  67. // vertices all gone; display sortedArray
  68. printf("Topologically sorted order: ");
  69. for (int j = 0; j < orig_nVerts; j++)
  70. printf(sortedArray[j]);
  71. printf("");
  72. }
  73.  
  74. public int noSuccessors() // returns vert with no successors (or -1 if no such verts)
  75. {
  76. boolean isEdge; // edge from row to column in adjMat
  77.  
  78. for (int row = 0; row < numVerts; row++) {
  79. isEdge = false; // check edges
  80. for (int col = 0; col < numVerts; col++) {
  81. if (matrix[row][col] > 0) // if edge to another,
  82. {
  83. isEdge = true;
  84. break;
  85. }
  86. }
  87. if (!isEdge)
  88. return row;
  89. }
  90. return -1;
  91. }
  92.  
  93. public void deleteVertex(int delVert) {
  94. if (delVert != numVerts - 1) // if not last vertex, delete from vertexList
  95. {
  96. for (int j = delVert; j < numVerts - 1; j++)
  97. vertexList[j] = vertexList[j + 1];
  98.  
  99. for (int row = delVert; row < numVerts - 1; row++)
  100. moveRowUp(row, numVerts);
  101.  
  102. for (int col = delVert; col < numVerts - 1; col++)
  103. moveColLeft(col, numVerts - 1);
  104. }
  105. numVerts--; // one less vertex
  106. }
  107.  
  108. private void moveRowUp(int row, int length) {
  109. for (int col = 0; col < length; col++)
  110. matrix[row][col] = matrix[row + 1][col];
  111. }
  112.  
  113. private void moveColLeft(int col, int length) {
  114. for (int row = 0; row < length; row++)
  115. matrix[row][col] = matrix[row][col + 1];
  116. }
  117.  
  118. public static void main(String[] args) {
  119. Graphs g = new Graphs();
  120. g.addVertex('A'); // 0
  121. g.addVertex('B'); // 1
  122. g.addVertex('C'); // 2
  123. g.addVertex('D'); // 3
  124. g.addVertex('E'); // 4
  125. g.addVertex('F'); // 5
  126. g.addVertex('G'); // 6
  127. g.addVertex('H'); // 7
  128.  
  129. g.addEdge(0, 3); // AD
  130. g.addEdge(0, 4); // AE
  131. g.addEdge(1, 4); // BE
  132. g.addEdge(2, 5); // CF
  133. g.addEdge(3, 6); // DG
  134. g.addEdge(4, 6); // EG
  135. g.addEdge(5, 7); // FH
  136. g.addEdge(6, 7); // GH
  137.  
  138. g.topo(); // do the sort
  139. }
  140. }

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 : '}'
Reputation Points: 10
Solved Threads: 0
Newbie Poster
largedimples is offline Offline
10 posts
since Jun 2009
Aug 15th, 2011
-1
Re: Topological Sort
Simply because you have copied the code (from here I think) and you don't know that this is written in Java and not in C.
Reputation Points: 6
Solved Threads: 0
Newbie Poster
thraxrules is offline Offline
1 posts
since Aug 2011
Aug 15th, 2011
0
Re: Topological Sort
This is an OOP based program, and C has no OOP support. So, you need to post this in either a Java, or maybe even a C++ forum. Definitely, not here.
Reputation Points: 416
Solved Threads: 181
Nearly a Posting Virtuoso
Adak is offline Offline
1,463 posts
since Jun 2008
Aug 15th, 2011
0
Re: Topological Sort
Resurrection from Oct 20th, 2009

Check the damm dates!!!!
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C Forum Timeline: Pointer to int, or pointer to _an_ int?
Next Thread in C Forum Timeline: help with debugging





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC