Question: I need help with the void addEdge(T source, T target); function. Please help with code needed to add edges to this adjacency list needed for

I need help with the "void addEdge(T source, T target);" function. Please help with code needed to add edges to this adjacency list needed for a depth first search in c++

Graph.h file:

#ifndef GRAPH_H

#define GRAPH_H

#include

#include

#include

#include

#include

using std::endl;

using std::cout;

using std::ostream;

using std::stack;

using std::vector;

using std::list;

template

class Graph;

template

ostream& operator << (ostream & out, Graph g);

template

class Graph{

private:

list* adjList; // array of lists

int V; // number of vertices

public:

Graph();

Graph(int);

void addVertex(T vertex);

void addEdge(T source, T target);

vector getPath(T, T);

int findVertexPos(T item);

int getNumVertices();

friend ostream& operator << <>(ostream & out, Graph g);

};

/*********************************************

* Constructs an empty graph with a max number of verticies of 100

*

*********************************************/

template

Graph::Graph(){

V = 100;

adjList = new list[V];

}

/*********************************************

* Constructs an empty graph with a given max number of verticies

*

*********************************************/

template

Graph::Graph(int maxVerticies){

V = maxVerticies;

adjList = new list[V];

}

/*********************************************

* Adds a Vertex to the GraphIf number of verticies is less than the

* Max Possible number of verticies.

*********************************************/

template

void Graph::addVertex(T vertex){

adjList->push_back(vertex);

}

/*********************************************

* Returns the current number of vertices

*

*********************************************/

template

int Graph::getNumVertices(){

return V;

}

/*********************************************

* Returns the position in the verticies list where the given vertex is located, -1 if not found.

*

*********************************************/

template

int Graph::findVertexPos(T item){

return -1; //return negative one

}//End findVertexPos

/*********************************************

* Adds an edge going in the direction of source going to target

*

*********************************************/

template

void Graph::addEdge(T source, T target) {

adjList[source].push_back(target);

}

/*********************************************

* Returns a display of the graph in the format

* vertex: edge edge

Your display will look something like the following

9: 8 5

2: 7 0

1: 4 0

7: 6 2

5: 6 8 9 4

4: 5 1

8: 6 5 9

3: 6 0

6: 7 8 5 3

0: 1 2 3

*********************************************/

template

ostream& operator << (ostream & out, Graph g){

return out;

}

/*

getPath will return the shortest path from source to dest.

You may use any traversal/search algorithm you want including

breadth first, depth first, dijkstra's algorithm, or any

other graph algorithm.

You will return a vector with the solution. The source will be in position 1

the destination is in the last position of the solution, and each node in between

are the verticies it will travel to get to the destination. There will not be any

other verticies in the list.

Given the test graph:

[0]-----------[2]

| \ \

| \ \

[1] [3]----[6]---[7]

| / \

| / \

| / \

| [5]--------[8]

| / \ /

| / \ /

| / \ /

[4] [9]

getPath(0, 5) should return

0 -> 1 -> 4 -> 5 or 0 -> 3 -> 6 -> 5

Hint: As you find the solution store it in a stack, pop all the items of the stack

into a vector so it will be in the correct order.

*/

template

vector Graph::getPath(T source, T dest){

vector solution;

return solution;

}

#endif

TEST CODE:

GraphpopulateNumbersGraph(){

Graphg(10);

int verts [] = {9, 2, 1, 7, 5, 4, 8, 3, 6, 0};

for(int i = 0; i < 10; i++){

g.addVertex(verts[i]);

}

g.addEdge(0, 1);

g.addEdge(0, 2);

g.addEdge(0, 3);

g.addEdge(1, 4);

g.addEdge(4, 5);

g.addEdge(5, 6);

g.addEdge(3, 6);

g.addEdge(6, 7);

g.addEdge(2, 7);

g.addEdge(6, 8);

g.addEdge(5, 8);

g.addEdge(5, 9);

g.addEdge(9, 8);

g.addEdge(1, 0);

g.addEdge(2, 0);

g.addEdge(3, 0);

g.addEdge(4, 1);

g.addEdge(5, 4);

g.addEdge(6, 5);

g.addEdge(6, 3);

g.addEdge(7, 6);

g.addEdge(7, 2);

g.addEdge(8, 6);

g.addEdge(8, 5);

g.addEdge(9, 5);

g.addEdge(8, 9);

return g;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!