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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
#include <iostream>
#include <list>
#include "Data.h"
#include <vector>
#include <queue>
using namespace std;
Graph::Graph(int x)
{
vector<pair<int, int> > adj[V];
}
void add_edge(vector <pair<int, int> > adj[], int u, int v, int wt)
{
adj[u].push_back(make_pair(v, wt));
adj[v].push_back(make_pair(u, wt));
}
void Graph::addEdge2(int u, int v)
{
adj2[u][v] = 1;
}
// A utility function to print the adjacency list
// representation of graph
void Graph::printGraph(vector<pair<int,int> > adj[], int V)
{
int v, w;
for (int u = 0; u < V; u++)
{
cout << "Node " << u << "->";
for (auto it = adj[u].begin(); it!=adj[u].end(); it++)
{
v = it->first;
w = it->second;
cout << v <<endl ;
}
cout << "";
}
}
void Graph::printGraph2()
{
cout << "Adjacency Matrix..." << endl << endl;
cout << "\t";
for (int i = 0; i < V; i++)
cout << "V[" << i << "]" << "\t";
cout << endl;
for (int i=0; i<V; i++)
{
cout << "V[" << i << "]" << "\t";
for (int j = 0; j < V; j++)
cout << adj2[i][j] << "\t";
cout << endl;
}
cout << endl;
}
void Graph::DFSUtil(int v, bool visited[])
{
// Mark the current node as visited and
// print it
visited[v] = true;
cout << v << " ";
// Recur for all the vertices adjacent
// to this vertex
list<int>::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i)
if (!visited[*i])
DFSUtil(*i, visited);
}
// DFS traversal of the vertices reachable from v.
// It uses recursive DFSUtil()
void Graph::DFS(int v)
{
// Mark all the vertices as not visited
bool *visited = new bool[V];
for (int i = 0; i < V; i++)
visited[i] = false;
// Call the recursive helper function
// to print DFS traversal
DFSUtil(v, visited);
for(int i=0; i< V; i++)
if (!visited[i])
DFSUtil(i, visited);
for (int i = 0; i < V; i++)
if (!visited[i])
cout << i << " ";
}
void Graph::BFS(int s)
{
// Mark all the vertices as not visited
bool *visited = new bool[V];
for (int i = 0; i < V; i++)
visited[i] = false;
BFSUtil(s, visited);
for (int i = 0; i < V; i++)
if (!visited[i])
BFSUtil(i, visited);
for (int i = 0; i < V; i++)
if (!visited[i])
cout << i << " ";
}
void Graph::BFSUtil(int s, bool visited[])
{
// Create a queue for BFS
list<int> queue;
// Mark the current node as visited and enqueue it
visited[s] = true;
queue.push_back(s);
// 'i' will be used to get all adjacent
// vertices of a vertex
list<int>::iterator i;
while (!queue.empty())
{
// Dequeue a vertex from queue and print it
s = queue.front();
cout << s << " ";
queue.pop_front();
// Get all adjacent vertices of the dequeued
// vertex s. If a adjacent has not been visited,
// then mark it visited and enqueue it
for (i = adj[s].begin(); i != adj[s].end(); ++i)
{
if (!visited[*i])
{
visited[*i] = true;
queue.push_back(*i);
}
}
}
}
bool Graph::isReachable(int s,int d)
{
// Base case
if(s == d)
return true;
int n= (int)adj->size();
// Mark all the vertices as not visited
vector<bool> visited(n,false);
// Create a queue for BFS
queue<int> q;
visited[s]= true;
q.push(s);
while(!q.empty())
{
// Dequeue a vertex from queue and print it
s=q.front();
q.pop();
// Get all adjacent vertices of the dequeued vertex s
// If a adjacent has not been visited, then mark it
// visited and enqueue it
for(auto x:adj[s])
{
// If this adjacent node is the destination node,
// then return true
if(x == d)
return true;
// Else, continue to do BFS
if(!visited[x])
{
visited[x] = true;
q.push(x);
}
}
}
// If BFS is complete without visiting d
return false;
}
//////////////////////////////////////////////////////////
// To print the shortest path stored in parent[]
int Graph::printShortestPath(int parent[], int s, int d)
{
static int level = 0;
// If we reached root of shortest path tree
if (parent[s] == -1)
{
cout << "Shortest Path between " << s << " and "
<< d << " is " << s << " ";
return level;
}
printShortestPath(parent, parent[s], d);
level++;
if (s < V)
cout << s << " ";
return level;
}
// This function mainly does BFS and prints the
// shortest path from src to dest. It is assumed
// that weight of every edge is 1
int Graph::findShortestPath(int src, int dest)
{
// Mark all the vertices as not visited
bool *visited = new bool[2*V];
int *parent = new int[2*V];
// Initialize parent[] and visited[]
for (int i = 0; i < 2*V; i++)
{
visited[i] = false;
parent[i] = -1;
}
// Create a queue for BFS
list<int> queue;
// Mark the current node as visited and enqueue it
visited[src] = true;
queue.push_back(src);
// 'i' will be used to get all adjacent vertices of a vertex
list<int>::iterator i;
while (!queue.empty())
{
// Dequeue a vertex from queue and print it
int s = queue.front();
if (s == dest)
return printShortestPath(parent, s, dest);
queue.pop_front();
// Get all adjacent vertices of the dequeued vertex s
// If a adjacent has not been visited, then mark it
// visited and enqueue it
for (i = adj[s].begin(); i != adj[s].end(); ++i)
{
if (!visited[*i])
{
visited[*i] = true;
queue.push_back(*i);
parent[*i] = s;
}
}
}
}
|