




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





Fall 2015- 2016 CSC2211 Algorithms |
Graph::Graph(int vertexCount) { this->vertexCount = vertexCount; adjacencyMatrix = new bool*[vertexCount]; // if weighted //graph then also change here as well for (int i = 0; i < vertexCount; i++) { adjacencyMatrix[i] = new bool[vertexCount]; //for weighted, //graph also change here for (int j = 0; j < vertexCount; j++) adjacencyMatrix[i][j] = false; // if no weight then place - 1 } } Graph::~Graph() { for (int i = 0; i < vertexCount; i++) delete[] adjacencyMatrix[i]; delete[] adjacencyMatrix; } #include
void Graph::DFS() { VertexState *state = new VertexState[vertexCount]; for (int i = 0; i < vertexCount; i++) state[i] = White; runDFS(0, state); delete [] state; } void Graph::runDFS(int u, VertexState state[]) { state[u] = Gray; for (int v = 0; v < vertexCount; v++) if (isEdge(u, v) && state[v] == White) runDFS(v, state); state[u] = Black; cout<<u<<","; }