Добавил:
yermolenkoigor9
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Diskret / Graph_in_deep / main
.cpp#include "stdafx.h"
#include <iostream>
using namespace std;
const int N = 9;
//матрица смежности графа
// 0 1 2 3 4 5 6 7 8
int Graf[N][N] ={{0,1,0,0,0,5,6,7,0},//0
{1,0,1,0,0,0,0,1,0},//1
{0,1,0,1,0,0,0,1,1},//2
{0,0,1,0,1,0,0,0,1},//3
{0,0,0,1,0,1,1,0,1},//4
{1,0,0,0,1,0,1,0,0},//5
{1,0,0,0,1,1,0,1,1},//6
{1,1,1,0,0,0,1,0,1},//7
{0,0,1,1,1,0,1,1,0}};//8
void DFS(int st, int n, bool *visited)
{
int r;
cout << st << " ";
visited[st] = true;
for (r=0; r <= n; r++)
if ((Graf[st][r]!=0) && (!visited[r]))
DFS(r,n,visited);
}
int main()
{
int start;
int i, j;
bool *visited=new bool[N];
cout << "The adjacency matrix of a graph: " << endl;
for (i=0; i<N; i++)
{
visited[i] = false;
for (j=0; j<N; j++)
cout<<" " << Graf[i][j];
cout<<endl;
}
cout<<"Input start point: "; cin>>start;
//массив посещенных вершин
cout << "Bypass order: ";
DFS(start,N,visited);
delete []visited;
cin.get();
return 0;
}