Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- #define N 4
- int viz[N];//varfuri deja vizitate
- struct graph {
- char v[N];
- int e[N][N];//edges
- };
- void DFS(int x,graph G)
- {
- int i;
- viz[x] = 1;
- for (i = 0;i < N;i++)
- {
- if (G.e[x][i] != 0 && viz[i] == 0)
- {
- //cout << G.e[x][i] << " ";
- DFS(i, G);
- }
- //cout << endl;
- }
- }
- int Conex(graph G)
- {
- int i;
- DFS(0,G);
- for (i = 0;i < N;i++)
- if (viz[i] == 0)
- return 0;
- return 1;
- }
- void printGraph(graph G)
- {
- for (int i=0;i<N+1;i++)
- {
- for (int j = 0;j < N + 1;j++)
- {
- if (i == 0 && j==0)
- {
- cout << " " << "\t";
- }
- else
- {
- if (i == 0)
- {
- cout << G.v[j-1] << "\t";
- }
- else
- {
- if (j == 0)
- {
- cout << G.v[i-1] << "\t";
- }
- else
- {
- cout << G.e[i-1][j-1] << "\t";
- }
- }
- }
- }
- cout << endl;
- cout << endl;
- }
- }
- int checkIfOriented(graph G)
- {
- int count = 0;
- int rand = 0;
- for (int i = 0;i < N-1;i++)
- {
- for (int j = i+1;j < N;j++)
- {
- if (G.e[i][j] == G.e[j][i])
- {
- count++;
- //cout << G.v[i] << " - " << G.v[j] << endl;
- }
- }
- }
- count = count * 2 + N;
- if (count == (N*N))
- {
- return 1;
- }
- return 0;
- }
- void printEdges(graph G)
- {
- for (int i = 0;i < N;i++)
- {
- for (int j = 0;j < N;j++)
- {
- if (G.e[i][j] !=0)
- {
- cout << G.v[i] << " - " << G.v[j] << endl;
- }
- }
- cout << endl;
- }
- }
- int main()
- {
- /*
- {{0,1,1,1},
- {1,0,0,1},
- {1,0,0,1},
- {1,1,1,1}}
- */
- /*
- {{0,0,1,0},
- {0,0,0,1},
- {1,0,0,0},
- {0,1,0,0}}
- */
- /*
- { { 0,1,0,0 },
- { 0,0,0,1 },
- { 1,1,1,0 },
- { 0,0,1,0 } }
- */
- graph G = {
- {'A','B','C','D'},
- { { 0,1,1,1 },
- { 1,0,0,1 },
- { 1,0,0,1 },
- { 1,1,1,1 } }
- };
- cout << "--------------------------------" << endl;
- printGraph(G);
- cout << "--------------------------------"<<endl;
- printEdges(G);
- cout << "--------------------------------"<<endl;
- if (Conex(G) == 0)
- {
- cout << "Graful nu este conex!" << endl;
- }
- else
- {
- cout << "Graful este conex!" << endl;
- }
- if (checkIfOriented(G) == 1)
- {
- cout << "Graful este neorientat!" << endl;
- }
- else
- {
- cout << "Graful este orientat!" << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement