Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define White 0
- #define Gray 2
- #define Black 3
- int row, col;
- int **graph;
- class Graph
- {
- private:
- bool** adjacencyMatrix;
- int vertexCount;
- public:
- Graph(int vertexCount)
- {
- this->vertexCount = vertexCount;
- adjacencyMatrix = new bool*[vertexCount];
- for (int i = 0; i < vertexCount; i++)
- {
- adjacencyMatrix[i] = new bool[vertexCount];
- for (int j = 0; j < vertexCount; j++)
- adjacencyMatrix[i][j] = false;
- }
- }
- Graph(const char* filename, int vertexCount)
- {
- this->vertexCount = vertexCount;
- adjacencyMatrix = new bool*[vertexCount];
- ifstream file;
- file.open(filename, ios::in);
- if( !file)
- {
- cout << "\nError: Cannot open file\n";
- return;
- }
- if(file.is_open())
- {
- for (int i = 0; i < vertexCount; i++)
- {
- adjacencyMatrix[i] = new bool[vertexCount];
- for (int j = 0; j < vertexCount; j++)
- file>>adjacencyMatrix[i][j];
- }
- }
- }
- ~Graph()
- {
- for (int i = 0; i < vertexCount; i++)
- delete[] adjacencyMatrix[i];
- delete[] adjacencyMatrix;
- }
- void runDFS(int u, int 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<<",";
- }
- void DFS()
- {
- int *state = new int[vertexCount];
- for (int i = 0; i < vertexCount; i++)
- state[i] = White;
- runDFS(0, state);
- delete [] state;
- }
- void display()
- {
- int u,v;
- for(u=0; u<vertexCount; ++u)
- {
- cout << "\nadj[" << (char) (u+65) << "] -> ";
- for(v=0; v<vertexCount; ++v)
- {
- cout << " " << adjacencyMatrix[u][v];
- }
- }
- cout << "\n\n";
- }
- void writeFile()
- {
- FILE *p;
- p = fopen("graph.txt", "w+");
- int u,v;
- for(u=0; u<vertexCount; ++u)
- {
- for(v=0; v<vertexCount; ++v)
- {
- fprintf(p, "%d ", adjacencyMatrix[u][v]);
- }
- fprintf(p, "\n");
- }
- fclose(p);
- }
- bool isEdge(int i, int j)
- {
- if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount)
- return adjacencyMatrix[i][j];
- else
- return false;
- }
- void removeEdge(int i, int j)
- {
- if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount)
- {
- adjacencyMatrix[i][j] = false;
- adjacencyMatrix[j][i] = false;
- }
- }
- void addEdge(int i, int j)
- {
- if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount)
- {
- adjacencyMatrix[i][j] = true;
- adjacencyMatrix[j][i] = true;
- }
- }
- };
- int main()
- {
- graph = new int*[row];
- for (int i = 0; i < row; i++)
- {
- graph[i] = new int[col];
- }
- for (int i = 0; i < row; i++)
- {
- for (int j = 0; j < col; j++)
- {
- graph[i][j] = 0;
- }
- }
- Graph* A = new Graph(5);
- printf("Task 1:\n");
- A->addEdge(0,3);
- A->addEdge(1,3);
- A->addEdge(1,4);
- A->addEdge(2,4);
- A->addEdge(3,0);
- A->addEdge(3,1);
- A->addEdge(3,4);
- A->addEdge(4,1);
- A->addEdge(4,2);
- A->addEdge(4,3);
- A->display();
- printf("Task 2:\n");
- A->writeFile();
- printf("\nA file named graph.txt is generated\n");
- printf("\nFinal task:\n\nReading the graph.txt file:\n\n");
- Graph* B = new Graph("graph.txt", 5);
- B->display();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement