how to traverse a graph?

hi im trying to make a program that will get a graph inputed from the the user and then checks if it has a cycle. i have written the program but for some reason i cant get it to check for the cycle it starts but somewhere it stops some help would be much appreciated :)

here is my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include<iostream>
using namespace std;
int main()
{
	int rows, columns, edges;
	cout<<"Please enter the amount of vertices: ";
	cin>>rows;
	char** matrix;   // makes this array into a pointer
	matrix=new char*[rows]; // to initialize the rows (1 dimensional array for now)
	cout<<"please enter each vertex"<<endl;
	columns=rows; // you cant have more edges than there are vertices so
	              // I made the matrix basicaly rows x rows size but named 
                  //it columns to differentiate
		
	//define the matrix
    for(int i=0;i<rows;i++)//in order to go through the whole 2-d array need to account for every location
	{
		matrix[i]=new char[columns]; //creating the 2-d part of the array or the columns
		for(int j=0;j<columns;j++)   // start at row 0, column 0 then goes to row 0, column 1.... etc.
		{
			cout<<"Enter the vertex "<<(i+1)<<": ";//enter a vertex(only one at a time) then follow below
			cin>>matrix[i][j]; // inputs that vertex into the certain location (ex- when start first goes into row 0, column 0...etc.)
                 cout<<"how many edges: ";//each vertex can have 1 or more edges or 0
                 cin>>edges;
				 j++;// need to move down one row in order to put in the vertex corresponding edges
                 while(j<edges+1)// this loop is based on the amount of edges the user inputed and will repeat that many times
                 {
                     cout<<"what is edge "<<j<<": ";
                     cin>>matrix[i][j];
					 j++;//need to move down one row in order to put in the vertex corresponding edges
					
                 }
            
			      while(j<columns)//this loop is used to fill all other spots that are left in the matrix with 0's or empty spaces
				  {
                       matrix[i][j]=0;
					   j++;
				  }
            cout<<"--------------------------------------------------------"<<endl;// seperate each set of vertices and their edges inputed. looks cleaner
         }
         }                   
    
    
    
     // starting from here is to print out the matrix vetices are the first row and each column has vertex followed by edges
	int i=0; //needed to initialize i and j since the for loop only initializes for the loop itself
	int j=0;// this is out the loop
	while(j<rows)//this loop is the same as the for loop above basically
	{
                 cout<<matrix[i][j]<<" ";
                 i++;
                 while(i<rows)
                 {
                    cout<<matrix[i][j]<<" ";
                    i++;
                 }
                 j++;
                 i=0;// need to reset i in order to start from the very top
                 cout<<endl;
                 }
     cout<<endl;
     cout<<"**The first row is the vertices and under each vertex are the edges**";
	cout<<"--------------------------------------------------------"<<endl;
	
	
	


	// start of the cycle check
	
	char check;// check is used to jump from vertex to next node and compare the next vertex with other nodes
	char vertex;// the first vertex we start off with in position [0][0]
	int exit=0;//use this in order to exit the while loop later in the program
	cout<<"checking to see if cycle";
	j=0;
        for(i=0;i<columns;i++)//make the loop only go through the columns
        {
                vertex=matrix[i][j];
                j++;
                if(matrix[i][j]!='0')
                {
					
                check=matrix[i][j];
                j=0;//reset for search
                i=0;//reset for search
                int count=1;// count is made in order to keep track how many jumps made from node to node (1 because first jump made)
                while(exit<rows)
                {
                   if(check==matrix[i][j])//this finds the edge node and goes down its edges
                       j++; //use this to go down from the vertex to its edges
                   else 
                       i++;//use this to try the next vertex
                   if(vertex==matrix[i][j]&&count>1)
				   {
					   cout<<"this graph has a cycle!";
						   break;
				   }
                   if(vertex!=matrix[i][j]&&check!=matrix[i][j])// makes sure the next vertex choosen is different 
                   {
                        check=matrix[i][j];//set that new vertex into check
                        j=0;//restart for search
                        i=0;//restart for search
                        count++;// another jump is made
                   }
                   exit++;
				}
                j=0;  
               
				}
				else
				j=0;
        }
	
	
	system("pause");
	return 0;
}
Topic archived. No new replies allowed.