From 293bef0a4261a44aa7147b56294beb63ab6ae3ba Mon Sep 17 00:00:00 2001 From: Nikunj Taneja <32366458+underscoreorcus@users.noreply.github.com> Date: Tue, 2 Oct 2018 11:10:44 +0800 Subject: [PATCH 1/2] Create bellman_ford.cpp --- graphs/bellman_ford.cpp | 106 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 graphs/bellman_ford.cpp diff --git a/graphs/bellman_ford.cpp b/graphs/bellman_ford.cpp new file mode 100644 index 00000000..340cc6a4 --- /dev/null +++ b/graphs/bellman_ford.cpp @@ -0,0 +1,106 @@ +#include + +struct Edge +{ + int src, dest, weight; +}; + +struct Graph +{ + // V-> Number of vertices, E-> Number of edges + int V, E; + // graph is represented as an array of edges. + struct Edge* edge; +}; + +struct Graph* createGraph(int V, int E) +{ + struct Graph* graph = new Graph; + graph->V = V; + graph->E = E; + graph->edge = new Edge[E]; + return graph; +} + +void printArr(int dist[], int n) +{ + printf("Vertex Distance from Source\n"); + for (int i = 0; i < n; ++i) + printf("%d \t\t %d\n", i, dist[i]); +} + +void BellmanFord(struct Graph* graph, int src) +{ + int V = graph->V; + int E = graph->E; + int dist[V]; + + // Step 1: Initialize distances from src to all other vertices + // as INFINITE + for (int i = 0; i < V; i++) + dist[i] = INT_MAX; + dist[src] = 0; + + // Step 2: Relax all edges |V| - 1 times. A simple shortest + // path from src to any other vertex can have at-most |V| - 1 + // edges + for (int i = 1; i <= V-1; i++) + { + for (int j = 0; j < E; j++) + { + int u = graph->edge[j].src; + int v = graph->edge[j].dest; + int weight = graph->edge[j].weight; + if (dist[u] != INT_MAX && dist[u] + weight < dist[v]) + dist[v] = dist[u] + weight; + } + } + + // Step 3: check for negative-weight cycles. The above step + // guarantees shortest distances if graph doesn't contain + // negative weight cycle. If we get a shorter path, then there + // is a cycle. + for (int i = 0; i < E; i++) + { + int u = graph->edge[i].src; + int v = graph->edge[i].dest; + int weight = graph->edge[i].weight; + if (dist[u] != INT_MAX && dist[u] + weight < dist[v]) + printf("Graph contains negative weight cycle"); + } + printArr(dist, V); + return; +} +int main() +{ + /* Sample graph */ + int V = 5; + int E = 8; + struct Graph* graph = createGraph(V, E); + graph->edge[0].src = 0; + graph->edge[0].dest = 1; + graph->edge[0].weight = -1; + graph->edge[1].src = 0; + graph->edge[1].dest = 2; + graph->edge[1].weight = 4; + graph->edge[2].src = 1; + graph->edge[2].dest = 2; + graph->edge[2].weight = 3; + graph->edge[3].src = 1; + graph->edge[3].dest = 3; + graph->edge[3].weight = 2; + graph->edge[4].src = 1; + graph->edge[4].dest = 4; + graph->edge[4].weight = 2; + graph->edge[5].src = 3; + graph->edge[5].dest = 2; + graph->edge[5].weight = 5; + graph->edge[6].src = 3; + graph->edge[6].dest = 1; + graph->edge[6].weight = 1; + graph->edge[7].src = 4; + graph->edge[7].dest = 3; + graph->edge[7].weight = -3; + BellmanFord(graph, 0); + return 0; +} From 8202a15b0e3c17f41cc7f448c907350a68b5bf50 Mon Sep 17 00:00:00 2001 From: Nikunj Taneja <32366458+underscoreorcus@users.noreply.github.com> Date: Tue, 2 Oct 2018 11:15:01 +0800 Subject: [PATCH 2/2] Create bfs.cpp --- graphs/bfs.cpp | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 graphs/bfs.cpp diff --git a/graphs/bfs.cpp b/graphs/bfs.cpp new file mode 100644 index 00000000..a2480464 --- /dev/null +++ b/graphs/bfs.cpp @@ -0,0 +1,87 @@ +#include +#include + +using namespace std; + +class Graph +{ + int V; // No. of vertices + + // Pointer to an array containing adjacency + // lists + list *adj; +public: + Graph(int V); // Constructor + + // function to add an edge to graph + void addEdge(int v, int w); + + // prints BFS traversal from a given source s + void BFS(int s); +}; + +Graph::Graph(int V) +{ + this->V = V; + adj = new list[V]; +} + +void Graph::addEdge(int v, int w) +{ + adj[v].push_back(w); // Add w to v’s list. +} + +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; + + // Create a queue for BFS + list 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::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); + } + } + } +} + +int main() +{ + // Sample graph + Graph g(4); + g.addEdge(0, 1); + g.addEdge(0, 2); + g.addEdge(1, 2); + g.addEdge(2, 0); + g.addEdge(2, 3); + g.addEdge(3, 3); + cout << "Following is Breadth First Traversal " + << "(starting from vertex 2) \n"; + g.BFS(2); + return 0; +}