Graph

I want to implement BFS weighted graph by using adjacency matrix using vector.

I do not know what should be the members for my class and where to create adjacency matrix.

I will share with you the header file, and cpp file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
#ifndef BFS_H
#define BFS_H

class Graph{
	private:
		int n; // number of vertices 
		
	public:
		Graph(int n){
			this.n = n;
		}

		~Graph(){}

		void BSF(int s, int t);
		void display(int s);

};

#endif 




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


#include <iostream>
#include <vector>
using namespace std;

void BSF(int s, int t){
	// n is number of nodes in graph and s is root point

	// keep track of visited node

	bool *visited = new bool[n];
	
	for(int i = 0 ; i < n; i++) // Initialize all nodes as unvisited
	{

		visited[i] = false;
	}
/*
    -- What does the value 0 represent?
    -- Put first node on queue of nodes to visit
    s.d := 0
*/

	std::vector<int> Q;
	visited[s] = true;
	Q.push_back(s);

	cout << " The first vertex is " << s << endl;

//While there are more nodes to visit

	while(Q != -1){

		int current = Q.pop_up();

		for(int i =0 ; i < n; i++)
		{
			if(visited[i] == false)
			{
				n.distance = current.distance + 1;
				n.parent = current;
				Q.push_back(i);
			}
		}
	}

}
Topic archived. No new replies allowed.