#include <iostream> #include <iomanip> #include <stack> #include <queue> #include <vector> #include <fstream> using namespace std; class graph { public: graph(); graph(int); //graph(int, int); void setEdge(int src, int dst); void printGraph();//print graph void bfs(int);//Breadth First Search void dfs(int);//Depth First Search void bfsSpan(int);//Breadth First Search Spanning Tree void dfsSpan(int);//DepthFirt Search Spanning Tree private: //internal struct to represent the node of the adjecency list struct node { int val; node *next; }; int nodeCount; int edgeCount; //pointer to dynamically allocate an array of nodes node* list; }; graph::graph() { nodeCount = 0; edgeCount = 0; list = NULL; } graph::graph(int nodes) { int … |
+0 forum
3 | ||

I have written this code to find the cycle in graph public boolean hasCycle(int i, int[][] mat) { visited[i] = 1; // for( int i = 0; i < numNodes; i++ ) for (int j = 1; j < mat.length; j++) { if (mat[i][j] == 1 && i != j) { if (visited[j] == 1) return true; } else if (hasCycle(j, mat)) return true; } return false; } But it is not giving correct result. Can anyone help? |
+0 forum
2 | ||

I am trying to create a graph from a 2d matrix of booleans. When the index [i][j] == 1, I need to create a link or edge from node[i] to node[j]. I am doing some thing wrong. I have created a class file with a struct node and some basic operations. I have realized a pointer of the struct in not the correct way to go about this. A node may have more than one edge to other nodes, and one pointer is not going to link them. Please help a struggling programming student. My code and output is listed … |
+0 forum
0 | ||

I have to write a program that does BFS traversal. My code is mostly correct but i have an error somewhere. it prints repeated numbers in some cases and it's not supposed to. i'm not sure exactly what the problem is. I really hope someone will be able to help me fix it soon. I think the error is after the comment that says "filling BFS array". I dont know what i did wrong. PLEASE HELP!! This is the orignal question: Given an undirected graph and a source, please output the traversal order of BFS from the given source. If … |
+0 forum
0 | ||

Hello! I just want to ask you guys if anyone can help me reedit my program from BFS to DFS. I know how BFS works but i can't figure it out how to write the sintax. So i would please ask you guys if you could help me out. Heres code of my last program: #include <iostream> #include <queue> using namespace std; const int bela = 0; const int siva = 1; const int crna = 2; const int nil = -1; const int inf = 2147483647; char menu() { cout << "Iskanje z razvijanjem v sirino - izbira:"; cout … |
+0 forum
1 | ||

I am doing a program to do dfs traversal given the adjacency matrix. My code follows: [CODE] #include <iostream> #define SIZE 10 using namespace std; bool M[SIZE][SIZE]; int flag[SIZE]; int dfsNum[SIZE], num; int n; void dfs( int u, int p ) { flag[u] = 1; dfsNum[u] = num++; for( int v = 0; v < n; v++ ) if( M[u][v] && v != p ) { if( flag[v] == 0 ) { dfs( v, u ); } } flag[u] = 2; } int main() { cout<<"Enter number of vertices\n"; cin>>n; cout<<"Enter adjacency matrix"; for(int i=0;i<n;i++) for(int j=0;j<n;j++) cin>>M[i][j]; for( int … |
+0 forum
2 | ||

Hello, I am working on an assignment and I am so stuck and I have no clue how to get out. We are giving a maze and we are to find start and finish of the maze. The code is interpreted as giving the "walls" of the cell by looking at the binary representation, with 1's bit = North wall, 2's bit = East wall, 4's bit = South wall, and 8's bit = West wall. (Interpret "up" as North.) For example, code 13 means a cell with North, South, and West walls but no East wall, because 13 = … |
+0 forum
4 | ||

Hello guys, can somebody give me example of some simple DFS problem and solution (Source code). I just need to understand DFS algorithm. Thanks XTRobot |
+0 forum
1 | ||

Hi there. I need to write a recursive DFS function for graph which is implemented as follows: [CODE=c] struct nodes_arr { unsigned int position[50000]; unsigned int amount[50000]; node *next[50000]; } nodes_table[] = {0};[/CODE] [CODE=c]struct node { unsigned int position; unsigned int amount; node *next; };[/CODE] as a result i have a graph which for example looks like: [CODE]1:100->5:50->12:50 2:200->7:110->6:70->11:20 3:300->8:165->7:110 5:50->11:20->10:30 6:70 7:110->9:100 8:165 9:100 10:30 11:20 12:50 15:0->29:50 29:50[/CODE] where first column is nodes_table->position[i] (vertice) : nodes_table->amount[i] (pointless in my problem here), and then pointers to structures (nodes_table->next[i]) of nodes, if vertice links to another vertice. Everything was fine … |
+1 forum
0 |

The End.