Question
#ifndef GRAPH_H #define GRAPH_H #include #include namespace Graphs{ template class Node{ public: Node(T value): val(value){} T val; }; //no source -- used in adjacency list
#ifndef GRAPH_H
#define GRAPH_H
#include
#include
namespace Graphs{
template
public:
Node(T value): val(value){}
T val;
};
//no source -- used in adjacency list
template
public:
T source;
T target;
W weight;
DirectedEdge(T s, T t, W w): source(s),target(t),weight(w){}
};
template
class DirectedWeightedGraph{
typedef DirectedEdge
//std::map
std::map
public:
void addVertex(T v){
verts[v]=std::vector
}
void addEdge(T source, T target, W weight){
DirectedEdge
verts[source].push_back(e);
}
std::vector< DE > getEdges(T vert){
return verts[vert];
}
std::map< T, int> getIndexMap(){
std::map< T, int> im;
std::vector
for(int i = 0;i im[vs[i]]=i; } return im; } std::vector std::vector for(typename std::map vs.push_back(it->first); } return vs; } /*************************************** *Returns adjacency matrix * ****************************************/ std::vector std::vector std::vector int n = vs.size(); for(int i =0;i am.push_back(std::vector for(int j =0;j am[i].push_back(non_adj_filler); } } std::map for(T v:vs){ for(int i =0; i DE e = verts[v][i]; am[im[v]][im[e.target]] = e.weight; } } return am; } std::vector std::vector std::vector int n = vs.size(); for(int i =0;i bam.push_back(std::vector for(int j =0;j bam[i].push_back(false); } } std::map for(T v:vs){ for(int i =0; i DE e = verts[v][i]; bam[im[e.source]][im[e.target]] = true; } } return bam; } /*************************************** * Get Transitive closure * define this with Warshall's * careful: this is accessed like a regular matrix (row, then column) ********************************************/ std::vector std::vector return R; } /*************************************** * Get All Pairs Shortests Paths * define this with Floyd's * careful: this is accessed like a regular matrix (row, then column) ********************************************/ std::vector //parameter is to indicate "filler" std::vector //Floyd's doesn't start with a regular adjacency matrix (look at diagonal) return D; } }; template std::string adjMatrix2String(const std::vector using std::endl; std::stringstream ss; int setwidth = 4; ss<<"\t"/*setw(setwidth)*/<<" "; for(T v:vs) {ss << "\t"/*setw(setwidth)*/ < ss<<" "; for(int i = 0;i T v = vs[i]; ss<<"\t"/*setw(setwidth)*/< for(W w:am[i]) {ss << "\t"/*setw(setwidth)*/< ss<<" "; } return ss.str(); } } #endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started