how to build a solution to topological...
TRANSCRIPT
![Page 1: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/1.jpg)
How to build a solution to topological sort
A B
C
D
E
FG
H
![Page 2: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/2.jpg)
How to build a solution to topological sort
A B
C
D
E
FG
H
1. Identify objects
![Page 3: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/3.jpg)
How to build a solution to topological sort
A B
C
D
E
FG
H
1. Identify objects The - call them Nodes
![Page 4: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/4.jpg)
How to build a solution to topological sort
A B
C
D
E
FG
H
1. Identify objects The - call them NodesThe - call then arcs
![Page 5: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/5.jpg)
How to build a solution to topological sort
A B
C
D
E
FG
H
1. Identify objects The - call them NodesThe - call then arcs
2. Identify object properties
![Page 6: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/6.jpg)
How to build a solution to topological sort
A B
C
D
E
FG
H
1. Identify objects The - call them NodesThe - call then arcs
2. Identify object propertiesFor Nodes:
![Page 7: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/7.jpg)
How to build a solution to topological sort
A B
C
D
E
FG
H
1. Identify objects The - call them NodesThe - call then arcs
2. Identify object propertiesFor Nodes: list of dependent Nodes
![Page 8: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/8.jpg)
How to build a solution to topological sort
A B
C
D
E
FG
H
1. Identify objects The - call them NodesThe - call then arcs
2. Identify object propertiesFor Nodes: list of dependent Nodes
an identity
![Page 9: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/9.jpg)
How to build a solution to topological sort
A B
C
D
E
FG
H
1. Identify objects The - call them NodesThe - call then arcs
2. Identify object propertiesFor Nodes: list of dependent Nodes
an identitya color
![Page 10: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/10.jpg)
How to build a solution to topological sort
A B
C
D
E
FG
H
1. Identify objects The - call them NodesThe - call then arcs
2. Identify object propertiesFor Nodes: list of dependent Nodes
an identitya color
For arcs:
![Page 11: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/11.jpg)
How to build a solution to topological sort
A B
C
D
E
FG
H
1. Identify objects The - call them NodesThe - call then arcs
2. Identify object propertiesFor Nodes: list of dependent Nodes
an identitya color
For arcs: two nodes, one dependson another
![Page 12: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/12.jpg)
How to build a solution to topological sort
A B
C
D
E
FG
H
1. Identify objects The - call them NodesThe - call then arcs
2. Identify object propertiesFor Nodes: list of dependent Nodes
an identitya color
For arcs: two nodes, one dependson another
3. Identify operations on objects
![Page 13: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/13.jpg)
How to build a solution to topological sort
A B
C
D
E
FG
H
1. Identify objects The - call them NodesThe - call then arcs
2. Identify object propertiesFor Nodes: list of dependent Nodes
an identitya color
For arcs: two nodes, one dependson another
3. Identify operations on objectsFor Nodes:
![Page 14: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/14.jpg)
How to build a solution to topological sort
A B
C
D
E
FG
H
1. Identify objects The - call them NodesThe - call then arcs
2. Identify object propertiesFor Nodes: list of dependent Nodes
an identitya color
For arcs: two nodes, one dependson another
3. Identify operations on objectsFor Nodes: Output ID of all
dependent Nodes then outputID of “this” Node
![Page 15: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/15.jpg)
How to build a solution to topological sort
A B
C
D
E
FG
H
1. Identify objects The - call them NodesThe - call then arcs
2. Identify object propertiesFor Nodes: list of dependent Nodes
an identitya color
For arcs: two nodes, one dependson another
3. Identify operations on objectsFor Nodes: Output ID of all
dependent Nodes then outputID of “this” Node
For arcs:
![Page 16: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/16.jpg)
How to build a solution to topological sort
A B
C
D
E
FG
H
1. Identify objects The - call them NodesThe - call then arcs
2. Identify object propertiesFor Nodes: list of dependent Nodes
an identitya color
For arcs: two nodes, one dependson another
3. Identify operations on objectsFor Nodes: Output ID of all
dependent Nodes then outputID of “this” Node
For arcs: ???
![Page 17: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/17.jpg)
How to build a solution to topological sort
A B
C
D
E
FG
H
1. Identify objects The - call them NodesThe - call then arcs
2. Identify object propertiesFor Nodes: list of dependent Nodes
an identitya color
For arcs: two nodes, one dependson another
3. Identify operations on objectsFor Nodes: Output ID of all
dependent Nodes then outputID of “this” Node
For arcs: ???4. Consider a supervisor for reading data
from file and running the sort
![Page 18: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/18.jpg)
How to build a solution to topological sortHow do we want the main procedure to look?
int main (int argc, char **argv) {if (argc != 2) {
cerr << “Usage: “ << argv[0] << “ <file>\n”;exit(0);
}
Supervisor *supervisor = new Supervisor(argv[1]);supervisor>getNodes();supervisor>setDependencies();supervisor>sort();supervisor>printSolution();
}
![Page 19: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/19.jpg)
How to build a solution to topological sortBuild the class for making Node objects
![Page 20: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/20.jpg)
How to build a solution to topological sortBuild the class for making Node objects
class Node {
![Page 21: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/21.jpg)
How to build a solution to topological sortBuild the class for making Node objects
class Node {char *id; // Identity of the Node
![Page 22: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/22.jpg)
How to build a solution to topological sortChoices in making the dependency list
A
E
D
CB
Each object is unique, dependency list is a list of pointers to objects
![Page 23: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/23.jpg)
How to build a solution to topological sortChoices in making the dependency list
A
E
D
CB
Many copies of each object. Dependency list has actual objects.
E EDC
![Page 24: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/24.jpg)
How to build a solution to topological sortChoices in making the dependency list
A
E
D
CB
Many copies of each object. Dependency list has actual objects.
E EDC D
DC
![Page 25: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/25.jpg)
How to build a solution to topological sortChoices in making the dependency list
A
E
D
CB
How to keep track of visits to neighboring nodes?
E EDC D
DC
![Page 26: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/26.jpg)
How to build a solution to topological sortChoices in making the dependency list
A
E
D
CB
How to keep track of visits to neighboring nodes?Suppose A visits D
E EDC D
DC
![Page 27: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/27.jpg)
How to build a solution to topological sortChoices in making the dependency list
A
E
D
CB
How to keep track of visits to neighboring nodes?Suppose A visits D ... later A is output after all its dependencies
E EDC D
DC
![Page 28: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/28.jpg)
How to build a solution to topological sortChoices in making the dependency list
A
E
D
CB
How to keep track of visits to neighboring nodes?Then B is visited for the first time
E EDC D
DC
![Page 29: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/29.jpg)
How to build a solution to topological sortChoices in making the dependency list
A
E
D
CB
How to keep track of visits to neighboring nodes?Then B is visited for the first time – should avoid D, but D is blue
E EDC D
DC
![Page 30: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/30.jpg)
How to build a solution to topological sortChoices in making the dependency list
A
E
D
CB
How to keep track of visits to neighboring nodes?If pointers are used, this is not a problem!
E
![Page 31: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/31.jpg)
How to build a solution to topological sortBuild the class for making Node objects
class Node { char *id; // Identity of the Node Node **depends; // Array of pointers to Nodes
![Page 32: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/32.jpg)
How to build a solution to topological sortBuild the class for making Node objects
class Node { char *id; // Identity of the Node Node **depends; // Array of pointers to Nodes int ndepends; // Number of dependencies
![Page 33: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/33.jpg)
How to build a solution to topological sortBuild the class for making Node objects
class Node { char *id; // Identity of the Node Node **depends; // Array of pointers to Nodes int ndepends; // Number of dependencies Color color; // Values=BLUE, BROWN, YELLOW
![Page 34: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/34.jpg)
How to build a solution to topological sortBuild the class for making Node objects
class Node { char *id; // Identity of the Node Node **depends; // Array of pointers to Nodes int ndepends; // Number of dependencies Color color; // Values=BLUE, BROWN, YELLOW
void init (char *id) { ndepends = 0; // Initialize state this>id = new char[strlen(id)+1]; strncpy(this>id, id, strlen(id)); depends = NULL; color = BLUE; }
![Page 35: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/35.jpg)
How to build a solution to topological sortBuild the class for making Node objects
class Node { char *id; // Identity of the Node Node **depends; // Array of pointers to Nodes int ndepends; // Number of dependencies Color color; // Values=BLUE, BROWN, YELLOW
void init (char *id) { ndepends = 0; // Initialize state this>id = new char[strlen(id)+1]; strncpy(this>id, id, strlen(id)); depends = NULL; color = BLUE; }
~Node () { // free memory to be delete depends; // grabbed later }
![Page 36: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/36.jpg)
How to build a solution to topological sortBuild the class for making Node objects
class Node { char *id; // Identity of the Node Node **depends; // Array of pointers to Nodes int ndepends; // Number of dependencies Color color; // Values=BLUE, BROWN, YELLOW
void topo () { // Outputs all dependents then itself
![Page 37: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/37.jpg)
How to build a solution to topological sortBuild the class for making Node objects
class Node { char *id; // Identity of the Node Node **depends; // Array of pointers to Nodes int ndepends; // Number of dependencies Color color; // Values=BLUE, BROWN, YELLOW
void topo () { // Outputs all dependents then itself // Hard to conceive of how to do this // so we call on our best friend, recursion, // to help out.
![Page 38: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/38.jpg)
How to build a solution to topological sortBuild the class for making Node objects
class Node { char *id; // Identity of the Node Node **depends; // Array of pointers to Nodes int ndepends; // Number of dependencies Color color; // Values=BLUE, BROWN, YELLOW
void topo () { // Outputs all dependents then itself // Hard to conceive of how to do this // so we call on our best friend, recursion, // to help out. // Assume everyone else has such a topo // we can invoke the topos of all our direct // dependents, that insures they are all // output ahead of us (let them figure out how // to output themselves properly). Then we // output ourselves. What does this look like?
![Page 39: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/39.jpg)
How to build a solution to topological sortBuild the class for making Node objects
class Node { char *id; // Identity of the Node Node **depends; // Array of pointers to Nodes int ndepends; // Number of dependencies Color color; // Values=BLUE, BROWN, YELLOW
void topo () { // Outputs all dependents first for (int i=0 ; i < ndepends ; i++) depends[i]>topo(); // Output this Node's identity cout << “ “ << id; }
![Page 40: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/40.jpg)
How to build a solution to topological sortBuild the class for making Node objects
class Node { char *id; // Identity of the Node Node **depends; // Array of pointers to Nodes int ndepends; // Number of dependencies Color color; // Values=BLUE, BROWN, YELLOW
void topo () { // Outputs all dependents first for (int i=0 ; i < ndepends ; i++) depends[i]>topo(); // Output this Node's identity cout << “ “ << id; }
But what happens if someone calls our topo() when we are YELLOW?
![Page 41: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/41.jpg)
How to build a solution to topological sortBuild the class for making Node objects
class Node { char *id; // Identity of the Node Node **depends; // Array of pointers to Nodes int ndepends; // Number of dependencies Color color; // Values=BLUE, BROWN, YELLOW
void topo () { if (color == YELLOW) return; // finished // Outputs all dependents first for (int i=0 ; i < ndepends ; i++) depends[i]>topo(); // Output this Node's identity cout << “ “ << id; color = YELLOW; // Now finished }
Set color variable to YELLOW after done, test upon entry for quick return
![Page 42: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/42.jpg)
How to build a solution to topological sortBuild the class for making Node objects
class Node { char *id; // Identity of the Node Node **depends; // Array of pointers to Nodes int ndepends; // Number of dependencies Color color; // Values=BLUE, BROWN, YELLOW
void topo () { if (color == YELLOW) return; // finished // Outputs all dependents first for (int i=0 ; i < ndepends ; i++) depends[i]>topo(); // Output this Node's identity cout << “ “ << id; color = YELLOW; // Now finished }
But what if someone calls our topo() after we start and before we finish?
![Page 43: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/43.jpg)
How to build a solution to topological sortBuild the class for making Node objects
class Node { char *id; // Identity of the Node Node **depends; // Array of pointers to Nodes int ndepends; // Number of dependencies Color color; // Values=BLUE, BROWN, YELLOW
void topo () { if (color == YELLOW) return; // finished if (color == BROWN) exit(0); color = BROWN; for (int i=0 ; i < ndepends ; i++) depends[i]>topo(); cout << “ “ << id; color = YELLOW; // Now finished }
Set color to BROWN just before loop and test for color being BROWN
![Page 44: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/44.jpg)
How to build a solution to topological sortBuild the class for making Node objects
class Node { char *id; // Identity of the Node Node **depends; // Array of pointers to Nodes int ndepends; // Number of dependencies Color color; // Values=BLUE, BROWN, YELLOW
void topo () { if (color == YELLOW) return; // finished if (color == BROWN) exit(0); color = BROWN; for (int i=0 ; i < ndepends ; i++) depends[i]>topo(); cout << “ “ << id; color = YELLOW; // Now finished }};
![Page 45: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/45.jpg)
How to build a solution to topological sortBuild the class for making a supervisor (reads data from file and sorts)
class Supervisor {
![Page 46: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/46.jpg)
How to build a solution to topological sortBuild the class for making a supervisor (reads data from file and sorts)
class Supervisor { Node *nodes; // Will be an array of nodes int nnodes; // Will contain the array size
![Page 47: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/47.jpg)
How to build a solution to topological sortBuild the class for making a supervisor (reads data from file and sorts)
class Supervisor { Node *nodes; // Will be an array of nodes int nnodes; // Will contain the array size fstream fin; // File handle
![Page 48: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/48.jpg)
How to build a solution to topological sortBuild the class for making a supervisor (reads data from file and sorts)
class Supervisor { Node *nodes; // Will be an array of nodes int nnodes; // Will contain the array size fstream fin; // File handle
Supervisor (char *filename) { fin.open(filename, ios::in); if (fin.fail()) { cerr << “Cannot open “ << filename <<”\n”; exit(0); } }
![Page 49: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/49.jpg)
How to build a solution to topological sortBuild the class for making a supervisor (reads data from file and sorts)
class Supervisor { Node *nodes; // Will be an array of nodes int nnodes; // Will contain the array size fstream fin; // File handle
Supervisor (char *filename) { fin.open(filename, ios::in); if (fin.fail()) { cerr << “Cannot open “ << filename <<”\n”; exit(0); } }
~Supervisor () { delete nodes; }
![Page 50: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/50.jpg)
How to build a solution to topological sortBuild the class for making a supervisor (reads data from file and sorts)
class Supervisor { Node *nodes; // Will be an array of nodes int nnodes; // Will contain the array size fstream fin; // File handle
void getNodes () { // Get space for Nodes
Should we use an array of Nodes?
![Page 51: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/51.jpg)
How to build a solution to topological sortBuild the class for making a supervisor (reads data from file and sorts)
class Supervisor { Node *nodes; // Will be an array of nodes int nnodes; // Will contain the array size fstream fin; // File handle
void getNodes () { // Get space for Nodes
Should we use an array of Nodes? If so, we will be unable to use any constructor other than the default (no argument) one.
![Page 52: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/52.jpg)
How to build a solution to topological sortBuild the class for making a supervisor (reads data from file and sorts)
class Supervisor { Node *nodes; // Will be an array of nodes int nnodes; // Will contain the array size fstream fin; // File handle
void getNodes () { // Get space for Nodes
Should we use an array of Nodes? If so, we will be unable to use any constructor other than the default (no argument) one. Instead, we use the method void init (char *); that was defined in class Node earlier.
![Page 53: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/53.jpg)
How to build a solution to topological sortBuild the class for making a supervisor (reads data from file and sorts)
class Supervisor { Node *nodes; // Will be an array of nodes int nnodes; // Will contain the array size fstream fin; // File handle
void getNodes () { // Get space for Nodes // Count number of Node objects // Create the array of Nodes // Set the Node identities
![Page 54: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/54.jpg)
How to build a solution to topological sortBuild the class for making a supervisor (reads data from file and sorts)
class Supervisor { Node *nodes; // Will be an array of nodes int nnodes; // Will contain the array size fstream fin; // File handle
void getNodes () { // Get space for Nodes // Count number of Node objects char tok[1024]; for (nnodes=0 ; fin >> tok ; nnodes++) if (tok[0] == '') break; // Create the array of Nodes // Set the Node identities
![Page 55: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/55.jpg)
How to build a solution to topological sortBuild the class for making a supervisor (reads data from file and sorts)
class Supervisor { Node *nodes; // Will be an array of nodes int nnodes; // Will contain the array size fstream fin; // File handle
void getNodes () { // Get space for Nodes // Count number of Node objects char tok[1024]; for (nnodes=0 ; fin >> tok ; nnodes++) if (tok[0] == '') break; // Create the array of Nodes Nodes = new Node[nnodes]; // Set the Node identities
![Page 56: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/56.jpg)
How to build a solution to topological sortBuild the class for making a supervisor (reads data from file and sorts)
class Supervisor { Node *nodes; // Will be an array of nodes int nnodes; // Will contain the array size fstream fin; // File handle
void getNodes () { // Get space for Nodes // Count number of Node objects char tok[1024]; for (nnodes=0 ; fin >> tok ; nnodes++) if (tok[0] == '') break; // Create the array of Nodes Nodes = new Node[nnodes]; // Set the Node identities fin.seekg(0, ios::beg); for (int i=0; fin>>tok && tok[0] != ''; i++) nodes[i].init(tok); }
![Page 57: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/57.jpg)
How to build a solution to topological sortBuild the class for making a supervisor (reads data from file and sorts)
class Supervisor { Node *nodes; // Will be an array of nodes int nnodes; // Will contain the array size fstream fin; // File handle
void setDependency (int i) { // set depends list // for Node i
![Page 58: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/58.jpg)
How to build a solution to topological sortBuild the class for making a supervisor (reads data from file and sorts)
class Supervisor { Node *nodes; // Will be an array of nodes int nnodes; // Will contain the array size fstream fin; // File handle
void setDependency (int i) {
Observe we have to create the dependency list for the ith Node here in the supervisor. This means there will have to be a method added to class Node which allows the Supervisor to “hand off” the newly created dependency list. In class Node add: void setdeps(Node **deps, int ndeps) { ndepends = ndeps; depends = deps; }
![Page 59: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/59.jpg)
How to build a solution to topological sortBuild the class for making a supervisor (reads data from file and sorts)
class Supervisor { Node *nodes; // Will be an array of nodes int nnodes; // Will contain the array size fstream fin; // File handle
void setDependency (int i) {
Other considerations: Use a variable char tok[1024]; to receive tokens from the file. Use size_t mark = fin.tellg(); fin.seekg(mark, ios::beg);
to remember the file cursor at the beginning of a line and then to return to that point later.
![Page 60: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/60.jpg)
How to build a solution to topological sortBuild the class for making a supervisor (reads data from file and sorts)
class Supervisor { Node *nodes; // Will be an array of nodes int nnodes; // Will contain the array size fstream fin; // File handle
void setDependency (int i) { // Save the file cursor (at beginning of line) // Count the number of dependencies on the line // Make dependency list: array of Node pointers // Cursor set to beginning of line // Reread line – set links – skip bogus links // Hand depends array off to the ith Node
![Page 61: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/61.jpg)
How to build a solution to topological sortBuild the class for making a supervisor (reads data from file and sorts)
class Supervisor { Node *nodes; // Will be an array of nodes int nnodes; // Will contain the array size fstream fin; // File handle
void setDependency (int i) { // Save the file cursor (at beginning of line) size_t mark = fin.tellg(); // Count the number of dependencies on the line // Make dependency list: array of Node pointers // Cursor set to beginning of line // Reread line – set links – skip bogus links // Hand depends array off to the ith Node
![Page 62: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/62.jpg)
How to build a solution to topological sortBuild the class for making a supervisor (reads data from file and sorts)
class Supervisor { Node *nodes; // Will be an array of nodes int nnodes; // Will contain the array size fstream fin; // File handle
void setDependency (int i) { // Save the file cursor (at beginning of line) size_t mark = fin.tellg(); // Count the number of dependencies on the line for (ndeps=0; fin>>tok && tok[0]!=''; ndeps++); // Make dependency list: array of Node pointers // Cursor set to beginning of line // Reread line – set links – skip bogus links // Hand depends array off to the ith Node
![Page 63: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/63.jpg)
How to build a solution to topological sortBuild the class for making a supervisor (reads data from file and sorts)
class Supervisor { Node *nodes; // Will be an array of nodes int nnodes; // Will contain the array size fstream fin; // File handle
void setDependency (int i) { // Save the file cursor (at beginning of line) size_t mark = fin.tellg(); // Count the number of dependencies on the line for (ndeps=0; fin>>tok && tok[0]!=''; ndeps++); // Make dependency list: array of Node pointers Node **deps = new Node*[ndeps]; // Cursor set to beginning of line // Reread line – set links – skip bogus links // Hand depends array off to the ith Node
![Page 64: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/64.jpg)
How to build a solution to topological sortBuild the class for making a supervisor (reads data from file and sorts)
class Supervisor { Node *nodes; // Will be an array of nodes int nnodes; // Will contain the array size fstream fin; // File handle
void setDependency (int i) { // Save the file cursor (at beginning of line) size_t mark = fin.tellg(); // Count the number of dependencies on the line for (ndeps=0; fin>>tok && tok[0]!=''; ndeps++); // Make dependency list: array of Node pointers Node **deps = new Node*[ndeps]; // Cursor set to beginning of line fin.seekg(mark, ios::beg); // Reread line – set links – skip bogus links // Hand depends array off to the ith Node
![Page 65: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/65.jpg)
How to build a solution to topological sortBuild the class for making a supervisor (reads data from file and sorts)
class Supervisor { Node *nodes; // Will be an array of nodes int nnodes; // Will contain the array size fstream fin; // File handle
void setDependency (int i) { size_t mark = fin.tellg(); for (ndeps=0; fin>>tok && tok[0]!=''; ndeps++); Node **deps = new Node*[ndeps]; fin.seekg(mark, ios::beg); // Reread line – set links – skip bogus for (ndeps=0 ; fin>>tok && tok[0] != '' ; ) { int n=atoi(tok); if (0<=n && n<nnodes) deps[ndeps++]=&nodes[n];
} // Hand depends array off to the ith Node
![Page 66: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/66.jpg)
How to build a solution to topological sortBuild the class for making a supervisor (reads data from file and sorts)
class Supervisor { Node *nodes; // Will be an array of nodes int nnodes; // Will contain the array size fstream fin; // File handle
void setDependency (int i) { size_t mark = fin.tellg(); for (ndeps=0; fin>>tok && tok[0]!=''; ndeps++); Node **deps = new Node*[ndeps]; fin.seekg(mark, ios::beg); for (ndeps=0 ; fin>>tok && tok[0] != '' ; ) { int n=atoi(tok); if (0<=n && n<nnodes) deps[ndeps++]=&nodes[n];
} // Hand depends array off to the ith Node nodes[i].setdeps(deps, ndeps); }
![Page 67: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/67.jpg)
How to build a solution to topological sortBuild the class for making a supervisor (reads data from file and sorts)
class Supervisor { Node *nodes; // Will be an array of nodes int nnodes; // Will contain the array size fstream fin; // File handle
void setDependency (int i) { size_t mark = fin.tellg(); for (ndeps=0; fin>>tok && tok[0]!=''; ndeps++); Node **deps = new Node*[ndeps]; fin.seekg(mark, ios::beg); for (ndeps=0 ; fin>>tok && tok[0] != '' ; ) { int n=atoi(tok); if (0<=n && n<nnodes) deps[ndeps++]=&nodes[n]; } nodes[i].setdeps(deps, ndeps); }
![Page 68: How to build a solution to topological sortgauss.ececs.uc.edu/Courses/c110/lectures/Week2/Topo/buildtopo.pdf · How to build a solution to topological sort A B C D E F G H 1. Identify](https://reader034.vdocuments.mx/reader034/viewer/2022050214/5f605c8ada134e4d587367ce/html5/thumbnails/68.jpg)
How to build a solution to topological sortBuild the class for making a supervisor (reads data from file and sorts)
class Supervisor { Node *nodes; // Will be an array of nodes int nnodes; // Will contain the array size fstream fin; // File handle
void setDependencies () { for (int i=0 ; i < nnodes ; i++) setDependency(i);
}
void sort () {for (int i=0 ; i < nnodes ; i++)
nodes[i].topo();cout << “\n”;
}};