D
D
ddddd tttt2017-04-15 11:14:02
Algorithms
ddddd tttt, 2017-04-15 11:14:02

How to implement a backtrack to find all paths between graph vertices?

How to remake the program so that a search is carried out in the graph between two vertices with a reverse move?

#include <stdio.h>
#include <conio.h>
#include<clocale>
#include<iostream>
#define n 5
char A[n][n] =
{ 
  1, 1, 1, 0, 1,
  1, 1, 1, 0, 0,
  1, 1, 1, 1, 0,
  0, 0, 1, 1, 1,
  1, 0, 0, 1, 1,
};
int mas[n] = { 0,0,0,0,0 };
using namespace std;
bool flag[n] = {false};
void DFS(int k, int prev, int cur)
{
  
    cout  << " - " << cur;
flag[cur] = true;
for (int i = 0; i < n; i++)
{
  if (i-1 == k) break;
  if (flag[i] == false && A[cur][i] == 1)
  {

    DFS(k, cur, i);
  }
}
}

int main()
{
int FST;
printf("Input number of list vertex: ");
scanf_s("%d", &FST);int k = 3;
cout << FST;
DFS(k, -1, FST);
_getch();
return 0;
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question