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
template
class Graph{
private:
list
int V; // number of vertices
public:
Graph();
Graph(int);
void addVertex(T vertex);
void addEdge(T source, T target);
vector
int findVertexPos(T item);
int getNumVertices();
friend ostream& operator << <>(ostream & out, Graph
};
/*********************************************
* Constructs an empty graph with a max number of verticies of 100
*
*********************************************/
template
Graph
V = 100;
adjList = new list
}
/*********************************************
* Constructs an empty graph with a given max number of verticies
*
*********************************************/
template
Graph
V = maxVerticies;
adjList = new list
}
/*********************************************
* Adds a Vertex to the GraphIf number of verticies is less than the
* Max Possible number of verticies.
*********************************************/
template
void Graph
adjList->push_back(vertex);
}
/*********************************************
* Returns the current number of vertices
*
*********************************************/
template
int Graph
return V;
}
/*********************************************
* Returns the position in the verticies list where the given vertex is located, -1 if not found.
*
*********************************************/
template
int Graph
return -1; //return negative one
}//End findVertexPos
/*********************************************
* Adds an edge going in the direction of source going to target
*
*********************************************/
template
void Graph
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
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
vector
return solution;
}
#endif
TEST CODE:
Graph
Graph
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
Get step-by-step solutions from verified subject matter experts
