|
-
June 25th, 2008, 11:28 AM
#1
cyclomatic complexity help needed
hey everyone i really need your help in any way possible.
i am new to c++ but i have a very hard assignment for my understanding.
i need to implement cyclomatic complexity.
i know that program needs to use the adj matrix like this for instance:
1 0 1 0
0 0 1 0
1 1 0 0
1 0 0 0
i need to use BFS to traverse through the nodes and find all the possible paths from beginning to the end as well as all the predicate nodes in this example there are 2 of them 1 0 1 0 and 1 1 0 0
so Cyclomatic Complexity should be 2 + 1 = 3
i actually managed to implement something so here it is:
Code:
#include <iostream>
using namespace std;
#define MAX 100
class BFS
{
private : int n;
int adj[MAX][MAX];
int visited[MAX];
public : void bfs(int);
void readmatrix();
};
void BFS :: readmatrix()
{
int i,j;
cout << "\nEnter the number of Vertices in the Graph : ";
cin >> n;
cout << "\nEnter the Adjacency Matrix\n\n";
for (i = 1; i <= n; i++)
for (j = 1; j<= n; j++)
cin >> adj[i][j];
for (i = 1; i <= n; i++)
visited[i] = 0;
}
void BFS :: bfs(int source)
{
int queue[MAX];
int i, front, rear, root;
front = rear = 0;
visited[source] = 1;
queue[rear++] = source;
cout << source << " ";
while (front != rear)
{
root = queue[front];
for (i = 1; i <= n; i++)
if (adj[root][i] && !visited[i])
{
visited[i] = 1;
queue[rear++] = i;
cout << i << " ";
}
front++;
}
}
int main()
{
int startNode;
BFS breadth;
breadth.readmatrix();
cout << "\nEnter the start node : ";
cin >> startNode;
cout << "\nThe nodes visited in the BFS order is : ";
breadth.bfs(startNode);
return 0;
}
this code asks you how many verticies the graph has then asks you to type up the matrix and then enter the start node...
after which it should print out all the paths possible... but in my case it prints out only one...
can someone please tell me what is wrong with my bfs ...
Last edited by eidalina20; June 25th, 2008 at 12:27 PM.
-
June 26th, 2008, 11:42 AM
#2
Re: cyclomatic complexity help needed
Looks to me like your algorithm is to find a single path. You set the nodes to visited, then once you've found a path it returns and doesn't look for any more paths.
What you should be doing instead is different. You need to keep track of paths, not just which nodes you've visited. I'd say try something like this:
Code:
Have a queue: todo. Create a path containing only the first node and put it in the queue.
While their are paths in the queue:
Take the first path (call it p) out of the queue.
If p ends with the node you want to get to, it is a path, put p in the list of paths.
For each adjacent node n (that is adjacent to the last element in p)
If n is not in p
copy p, put n at the end of the copy, add it at the end of the queue
Not certain if this will work or not, I just whipped it up here. It should get you on the right track though.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|