display neighbosr of each vertex

hi guys. How can create a code to display the beighbors of each vertex in a graph. for example the neighbor of 0 is 0,3
neighbor if 1 is 0. Youll see the neighbors in the matrix
Here is my code. Let me know if you need the header also.

Last edited on
:O is somebody trying to make an efficient Hamilton Circuit algorithm? :D

Make a class called Vertex, which has a member called std::string name, so that you can keep track of your vertices, then a std::vector<Vertex> neighbors of the neighbor points. Then make a class called Edge which has the members Vertex start, Vertex end, and int weight.
So then your prog would work like this:
1
2
3
4
5
6
7
graph<int> t;
Vertex a("A"); //param in constructor should set member "name" to "A";
Vertex b("B");
t.addVertex(a);
t.addVertex(b);
Edge a_to_b(a, b, 10); //a as start vertex, b as end vertex, 10 as weight
t.addEdge(a_to_b);

And for t.addEdge:
1
2
3
4
5
6
Graph::addEdge(Edge e)  //type: void
{
  graphEdges.push_back(e); //assuming all the edges are stores in a std::vector<Edge>
  t.addVertexNeighbor(e.start, e.end);
  t.addVertexNeighbor(e.end, e.start);
}

And for t.addVertexNeighbor:
1
2
3
4
Graph::addVertexNeighbor(Vertex update, Vertex neighbor) //type: void
{
  update.neighbors.push_back(neighbor);
}

Don't forget to use code tags!
http://www.cplusplus.com/forum/articles/16853/
this is insidde my graph.h

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
	class Vertex
	{

	public:
		 string name;
		 vector<Vertex> neighbors;

	};
class Edge
{
public:
	Vertex start;
	Vertex end;
	int weight;
graph::add_Edge(Edge e)  
{
  graphEdges.push_back(e); 
  t.addVertexNeighbor(e.start, e.end);
  t.addVertexNeighbor(e.end, e.start);
}
Graph::addVertexNeighbor(Vertex update, Vertex neighbor) //type: void
{
  update.neighbors.push_back(neighbor);
}


};
}
Last edited on
*cough* code tags *cough cough*
I'll check it out tomorrow as I'm on my phone atm
Topic archived. No new replies allowed.