Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void DFSHelp(int v, Adjacency* result,bool* visited){
- visited[v] = true;
- result->push(v);
- for (int i = 0; i < adj[v].getSize(); ++i){
- int index = adj[v].getElement(i);
- if (visited[index] == false) DFSHelp(index,result,visited);
- }
- return;
- }
- Adjacency *DFS(int v)
- {
- bool* visited = new bool[V];
- for (int i = 0; i < V; ++i){
- visited[i] = false;
- }
- Adjacency* result = new Adjacency();
- DFSHelp(v,result,visited);
- delete[] visited;
- return result;
- }
- ----------------------------------------
- // Some helping functions
- #define INF 1000
- int minDistance(vector<int>& dist, vector<bool>& supportSet){
- int min = INF;
- int min_index = 0;
- for (int i = 0; i < (int)dist.size(); ++i){
- if (supportSet[i] == false && dist[i] <= min){
- min = dist[i];
- min_index = i;
- }
- }
- return min_index;
- }
- int Dijkstra(int** graph, int src, int dst) {
- int size = *(&graph[0] + 1) - graph[0];
- vector<int> distance(size,INF); // to store distance from src to dst.
- /*return true if vertex i is included in shortest path or
- shortest distance from src to i is finalized.
- */
- vector<bool> supportSet(size,false);
- distance[src] = 0;
- for (int i = 0; i < size - 1; ++i){
- int u = minDistance(distance,supportSet);
- supportSet[u] = true;
- for (int v = 0; v < size; ++v ){
- if (supportSet[v] == false && graph[u][v] && distance[u] != INF
- && distance[u] + graph[u][v] < distance[v])
- distance[v] = distance[u] + graph[u][v];
- }
- }
- return distance[dst];
- // TODO: return the length of the shortest path from src to dst.
- }
- ------------------------------------------------------
- #include<iostream>
- #include <list>
- using namespace std;
- class DirectedGraph
- {
- int V;
- list<int> *adj;
- bool isCyclicUtil(int v, bool visited[], bool *rs);
- public:
- DirectedGraph(){
- V = 0;
- adj = NULL;
- }
- DirectedGraph(int V)
- {
- this->V = V;
- adj = new list<int>[V];
- }
- void addEdge(int v, int w)
- {
- adj[v].push_back(w);
- }
- bool isCyclic()
- {
- bool* visited = new bool[V];
- bool* recursionStack = new bool[V];
- for (int i = 0; i< V;++i){
- visited[i] = false;
- recursionStack[i] = false;
- }
- for (int i = 0; i < V; ++i){
- if (visited[i] == false && isCyclicUtil(i,visited,recursionStack) == true) {
- delete[] visited;
- delete[] recursionStack;
- return true;
- }
- }
- delete[] visited;
- delete[] recursionStack;
- return false;
- // Student answer
- }
- };
- bool DirectedGraph::isCyclicUtil(int v, bool visited[], bool *rs){
- if (visited[v] == false){
- visited[v] = true;
- rs[v] = true;
- for (auto& i : adj[v]){
- if (visited[i] == false && isCyclicUtil(i,visited,rs) == true) return true;
- else if (rs[i] == true) return true;
- }
- }
- rs[v] = false;
- return false;
- }
- --------------------------------------------------------------
- // Some helping functions
- int find_set(int i,int* parent){
- if (i == parent[i]) return i;
- return find_set(parent[i],parent);
- }
- void union_set(int u, int v, int* parent) {
- parent[u] = parent[v];
- }
- int kruskalMST() {
- vector< pair<int, pair<int, int>> > mst;
- int* parent = new int[V];
- for (int i = 0; i < V; i++) parent[i] = i;
- // TODO: return weight of the minimum spanning tree.
- int uRep, vRep;
- sort(edges.begin(),edges.end());
- for (int i = 0; i < (int)edges.size(); ++i){
- uRep = find_set(edges[i].second.first,parent);
- vRep = find_set(edges[i].second.second,parent);
- if (uRep != vRep){
- mst.push_back(edges[i]);
- union_set(uRep,vRep,parent);
- }
- }
- delete[] parent;
- int result = 0;
- for (auto& i : mst){
- result = result + i.first;
- }
- return result;
- }
- ===============================================
- class Graph {
- int V;
- Adjacency* adj;
- public:
- Graph(int V){
- this->V = V;
- adj = new Adjacency[V];
- }
- void addEdge(int v, int w){
- adj[v].push(w);
- }
- //Heling functions
- bool isCyclicUtil(int v, bool* visited, bool *rs){
- if (visited[v] == false){
- visited[v] = true;
- rs[v] = true;
- for (int i = 0; i < adj[v].getSize(); ++i){
- int index = adj[v].getElement(i);
- if (visited[index] == false && isCyclicUtil(index,visited,rs) == true) return true;
- else if (rs[index] == true) return true;
- }
- }
- rs[v] = false;
- return false;
- }
- bool isCyclic()
- {
- bool* visited = new bool[V];
- bool* recursionStack = new bool[V];
- for (int i = 0; i< V;++i){
- visited[i] = false;
- recursionStack[i] = false;
- }
- for (int i = 0; i < V; ++i){
- if (visited[i] == false && isCyclicUtil(i,visited,recursionStack) == true) {
- delete[] visited;
- delete[] recursionStack;
- return true;
- }
- }
- delete[] visited;
- delete[] recursionStack;
- return false;
- // Student answer
- }
- void DFSHelp(int v,bool* visited, stack<int>& result){
- visited[v] = true;
- for (int i = 0; i < adj[v].getSize(); ++i){
- int index = adj[v].getElement(i);
- if (visited[index] == false) DFSHelp(index,visited,result);
- }
- result.push(v);
- return;
- }
- void topologicalSort(){
- if (isCyclic() == true){
- return;
- }
- stack<int> result;
- bool* visited = new bool[V];
- for (int i = 0; i < V; ++i) visited[i] = false;
- for (int i = 0; i < V; ++i){
- if (visited[i] == false) DFSHelp(i,visited,result);
- }
- while (!result.empty()){
- cout << result.top() << " ";
- result.pop();
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement