From ae85f63a352061b5cb250b370526c7cf85e5c081 Mon Sep 17 00:00:00 2001 From: GanapathiMangipudi Date: Sat, 26 Oct 2019 12:29:45 +0530 Subject: [PATCH 1/3] added graph algorithm --- graph.cpp | 142 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 graph.cpp diff --git a/graph.cpp b/graph.cpp new file mode 100644 index 0000000..df5eb15 --- /dev/null +++ b/graph.cpp @@ -0,0 +1,142 @@ +// C++ program to find single source shortest paths for Directed Acyclic Graphs +#include +#include +#include +#include +#define INF INT_MAX +using namespace std; + +// Graph is represented using adjacency list. Every node of adjacency list +// contains vertex number of the vertex to which edge connects. It also +// contains weight of the edge +class AdjListNode +{ + int v; + int weight; +public: + AdjListNode(int _v, int _w) { v = _v; weight = _w;} + int getV() { return v; } + int getWeight() { return weight; } +}; + +// Class to represent a graph using adjacency list representation +class Graph +{ + int V; // No. of vertices' + + // Pointer to an array containing adjacency lists + list *adj; + + // A function used by shortestPath + void topologicalSortUtil(int v, bool visited[], stack &Stack); +public: + Graph(int V); // Constructor + + // function to add an edge to graph + void addEdge(int u, int v, int weight); + + // Finds shortest paths from given source vertex + void shortestPath(int s); +}; + +Graph::Graph(int V) +{ + this->V = V; + adj = new list[V]; +} + +void Graph::addEdge(int u, int v, int weight) +{ + AdjListNode node(v, weight); + adj[u].push_back(node); // Add v to u's list +} + +// A recursive function used by shortestPath. See below link for details +// https://www.geeksforgeeks.org/topological-sorting/ +void Graph::topologicalSortUtil(int v, bool visited[], stack &Stack) +{ + // Mark the current node as visited + visited[v] = true; + + // Recur for all the vertices adjacent to this vertex + list::iterator i; + for (i = adj[v].begin(); i != adj[v].end(); ++i) + { + AdjListNode node = *i; + if (!visited[node.getV()]) + topologicalSortUtil(node.getV(), visited, Stack); + } + + // Push current vertex to stack which stores topological sort + Stack.push(v); +} + +// The function to find shortest paths from given vertex. It uses recursive +// topologicalSortUtil() to get topological sorting of given graph. +void Graph::shortestPath(int s) +{ + stack Stack; + int dist[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 store Topological Sort + // starting from all vertices one by one + for (int i = 0; i < V; i++) + if (visited[i] == false) + topologicalSortUtil(i, visited, Stack); + + // Initialize distances to all vertices as infinite and distance + // to source as 0 + for (int i = 0; i < V; i++) + dist[i] = INF; + dist[s] = 0; + + // Process vertices in topological order + while (Stack.empty() == false) + { + // Get the next vertex from topological order + int u = Stack.top(); + Stack.pop(); + + // Update distances of all adjacent vertices + list::iterator i; + if (dist[u] != INF) + { + for (i = adj[u].begin(); i != adj[u].end(); ++i) + if (dist[i->getV()] > dist[u] + i->getWeight()) + dist[i->getV()] = dist[u] + i->getWeight(); + } + } + + // Print the calculated shortest distances + for (int i = 0; i < V; i++) + (dist[i] == INF)? cout << "INF ": cout << dist[i] << " "; +} + +// Driver program to test above functions +int main() +{ + // Create a graph given in the above diagram. Here vertex numbers are + // 0, 1, 2, 3, 4, 5 with following mappings: + // 0=r, 1=s, 2=t, 3=x, 4=y, 5=z + Graph g(6); + g.addEdge(0, 1, 5); + g.addEdge(0, 2, 3); + g.addEdge(1, 3, 6); + g.addEdge(1, 2, 2); + g.addEdge(2, 4, 4); + g.addEdge(2, 5, 2); + g.addEdge(2, 3, 7); + g.addEdge(3, 4, -1); + g.addEdge(4, 5, -2); + + int s = 1; + cout << "Following are shortest distances from source " << s <<" n"; + g.shortestPath(s); + + return 0; +} From 96d4cc8a69f7f103410e4e0295ec8a1a51f9bf38 Mon Sep 17 00:00:00 2001 From: GanapathiMangipudi <57028826+GanapathiMangipudi@users.noreply.github.com> Date: Wed, 9 Mar 2022 15:13:37 +0530 Subject: [PATCH 2/3] Delete README.md --- README.md | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 README.md diff --git a/README.md b/README.md deleted file mode 100644 index c3e050b..0000000 --- a/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# Data Structures and Algorithms in C - -## Won't count towards prize From a9fc2695c050aa5ff41e3f0ec916fef14b6d1698 Mon Sep 17 00:00:00 2001 From: GanapathiMangipudi <57028826+GanapathiMangipudi@users.noreply.github.com> Date: Wed, 9 Mar 2022 15:13:49 +0530 Subject: [PATCH 3/3] Delete graph.cpp --- graph.cpp | 142 ------------------------------------------------------ 1 file changed, 142 deletions(-) delete mode 100644 graph.cpp diff --git a/graph.cpp b/graph.cpp deleted file mode 100644 index df5eb15..0000000 --- a/graph.cpp +++ /dev/null @@ -1,142 +0,0 @@ -// C++ program to find single source shortest paths for Directed Acyclic Graphs -#include -#include -#include -#include -#define INF INT_MAX -using namespace std; - -// Graph is represented using adjacency list. Every node of adjacency list -// contains vertex number of the vertex to which edge connects. It also -// contains weight of the edge -class AdjListNode -{ - int v; - int weight; -public: - AdjListNode(int _v, int _w) { v = _v; weight = _w;} - int getV() { return v; } - int getWeight() { return weight; } -}; - -// Class to represent a graph using adjacency list representation -class Graph -{ - int V; // No. of vertices' - - // Pointer to an array containing adjacency lists - list *adj; - - // A function used by shortestPath - void topologicalSortUtil(int v, bool visited[], stack &Stack); -public: - Graph(int V); // Constructor - - // function to add an edge to graph - void addEdge(int u, int v, int weight); - - // Finds shortest paths from given source vertex - void shortestPath(int s); -}; - -Graph::Graph(int V) -{ - this->V = V; - adj = new list[V]; -} - -void Graph::addEdge(int u, int v, int weight) -{ - AdjListNode node(v, weight); - adj[u].push_back(node); // Add v to u's list -} - -// A recursive function used by shortestPath. See below link for details -// https://www.geeksforgeeks.org/topological-sorting/ -void Graph::topologicalSortUtil(int v, bool visited[], stack &Stack) -{ - // Mark the current node as visited - visited[v] = true; - - // Recur for all the vertices adjacent to this vertex - list::iterator i; - for (i = adj[v].begin(); i != adj[v].end(); ++i) - { - AdjListNode node = *i; - if (!visited[node.getV()]) - topologicalSortUtil(node.getV(), visited, Stack); - } - - // Push current vertex to stack which stores topological sort - Stack.push(v); -} - -// The function to find shortest paths from given vertex. It uses recursive -// topologicalSortUtil() to get topological sorting of given graph. -void Graph::shortestPath(int s) -{ - stack Stack; - int dist[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 store Topological Sort - // starting from all vertices one by one - for (int i = 0; i < V; i++) - if (visited[i] == false) - topologicalSortUtil(i, visited, Stack); - - // Initialize distances to all vertices as infinite and distance - // to source as 0 - for (int i = 0; i < V; i++) - dist[i] = INF; - dist[s] = 0; - - // Process vertices in topological order - while (Stack.empty() == false) - { - // Get the next vertex from topological order - int u = Stack.top(); - Stack.pop(); - - // Update distances of all adjacent vertices - list::iterator i; - if (dist[u] != INF) - { - for (i = adj[u].begin(); i != adj[u].end(); ++i) - if (dist[i->getV()] > dist[u] + i->getWeight()) - dist[i->getV()] = dist[u] + i->getWeight(); - } - } - - // Print the calculated shortest distances - for (int i = 0; i < V; i++) - (dist[i] == INF)? cout << "INF ": cout << dist[i] << " "; -} - -// Driver program to test above functions -int main() -{ - // Create a graph given in the above diagram. Here vertex numbers are - // 0, 1, 2, 3, 4, 5 with following mappings: - // 0=r, 1=s, 2=t, 3=x, 4=y, 5=z - Graph g(6); - g.addEdge(0, 1, 5); - g.addEdge(0, 2, 3); - g.addEdge(1, 3, 6); - g.addEdge(1, 2, 2); - g.addEdge(2, 4, 4); - g.addEdge(2, 5, 2); - g.addEdge(2, 3, 7); - g.addEdge(3, 4, -1); - g.addEdge(4, 5, -2); - - int s = 1; - cout << "Following are shortest distances from source " << s <<" n"; - g.shortestPath(s); - - return 0; -}