Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into ternary_search_javascript
- Loading branch information
Showing
290 changed files
with
4,458 additions
and
3,552 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
--- | ||
name: Bug report | ||
about: Create a report to help us improve | ||
|
||
--- | ||
|
||
**Describe the bug** | ||
A clear and concise description of what the bug is. | ||
|
||
**To Reproduce** | ||
Steps to reproduce the behavior: | ||
1. Go to '...' | ||
2. Click on '....' | ||
3. Scroll down to '....' | ||
4. See error | ||
|
||
**Expected behavior** | ||
A clear and concise description of what you expected to happen. | ||
|
||
**Screenshots** | ||
If applicable, add screenshots to help explain your problem. | ||
|
||
**Desktop (please complete the following information):** | ||
- OS: [e.g. iOS] | ||
- Browser [e.g. chrome, safari] | ||
- Version [e.g. 22] | ||
|
||
**Smartphone (please complete the following information):** | ||
- Device: [e.g. iPhone6] | ||
- OS: [e.g. iOS8.1] | ||
- Browser [e.g. stock browser, safari] | ||
- Version [e.g. 22] | ||
|
||
**Additional context** | ||
Add any other context about the problem here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- | ||
name: Feature request | ||
about: Suggest an idea for this project | ||
|
||
--- | ||
|
||
**Is your feature request related to a problem? Please describe.** | ||
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] | ||
|
||
**Describe the solution you'd like** | ||
A clear and concise description of what you want to happen. | ||
|
||
**Describe alternatives you've considered** | ||
A clear and concise description of any alternative solutions or features you've considered. | ||
|
||
**Additional context** | ||
Add any other context or screenshots about the feature request here. |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Large diffs are not rendered by default.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Bellman_Ford_Algorithm | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
int noOfVertices = 5; | ||
int noOfEdges = 7; | ||
Graph graph = new Graph(noOfVertices, noOfEdges); | ||
graph.edgesList.Add(new Edge(0, 1, 4)); | ||
graph.edgesList.Add(new Edge(0, 2, 5)); | ||
graph.edgesList.Add(new Edge(0, 3, 8)); | ||
graph.edgesList.Add(new Edge(1, 2, -3)); | ||
graph.edgesList.Add(new Edge(2, 4, 4)); | ||
graph.edgesList.Add(new Edge(3, 4, 2)); | ||
graph.edgesList.Add(new Edge(4, 3, 1)); | ||
//int noOfVertices = 5; | ||
//int noOfEdges = 7; | ||
//Graph graph = new Graph(noOfVertices, noOfEdges); | ||
//graph.edgesList.Add(new Edge(0, 1, -1));1 | ||
//graph.edgesList.Add(new Edge(0, 2, 4)); | ||
//graph.edgesList.Add(new Edge(1, 2, 3)); | ||
//graph.edgesList.Add(new Edge(1, 3, 2)); | ||
//graph.edgesList.Add(new Edge(1, 4, 2)); | ||
//graph.edgesList.Add(new Edge(3, 2, 5)); | ||
//graph.edgesList.Add(new Edge(3, 1, 1)); | ||
//graph.edgesList.Add(new Edge(4, 3, -3)); | ||
|
||
BellmanFord(graph, 0); | ||
|
||
Console.ReadLine(); | ||
|
||
} | ||
|
||
public static void BellmanFord(Graph g, int src) | ||
{ | ||
int V = g.vertices; | ||
int E = g.edges; | ||
int[] distance = new int[V]; | ||
int[] parent = new int[V]; | ||
|
||
for (int i = 0; i < V; i++) | ||
distance[i] = int.MaxValue; | ||
|
||
distance[src] = 0; | ||
|
||
for (int i = 1; i <= V-1; i++) | ||
{ | ||
for (int j = 0; j < E; j++) | ||
{ | ||
int u = g.edgesList[j].src; | ||
int v = g.edgesList[j].dest; | ||
int weight = g.edgesList[j].weight; | ||
|
||
if (distance[u] != int.MaxValue && distance[u] + weight < distance[v]) | ||
{ | ||
distance[v] = distance[u] + weight; | ||
parent[v] = u; | ||
} | ||
|
||
} | ||
} | ||
|
||
printArr(distance, parent, V); | ||
|
||
} | ||
|
||
private static void printArr(int[] distance, int[] parent, int v) | ||
{ | ||
Console.WriteLine("Vertex \t Distance \t Parent"); | ||
for (int i = 0; i < v; ++i) | ||
Console.WriteLine("{0} \t\t {1} \t\t {2} \n", i, distance[i], parent[i]); | ||
} | ||
} | ||
|
||
public class Graph | ||
{ | ||
public List<Edge> edgesList; | ||
public int vertices, edges; | ||
|
||
public LinkedList<int>[] adjList; | ||
|
||
public Graph(int vertices, int edges) | ||
{ | ||
this.vertices = vertices; | ||
this.edges = edges; | ||
edgesList = new List<Edge>(edges); | ||
} | ||
|
||
} | ||
|
||
public class Edge | ||
{ | ||
public int src, dest; | ||
public int weight; | ||
|
||
public Edge(int src, int dest, int weight) | ||
{ | ||
this.src = src; | ||
this.dest = dest; | ||
this.weight = weight; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#include <stdio.h> | ||
#include <vector> | ||
#include <queue> | ||
#include <algorithm> | ||
#include <iostream> | ||
#include <unordered_set> | ||
|
||
#define NMax 100010 | ||
#define INF 9999 | ||
using namespace std; | ||
|
||
int N, M; | ||
int Parent[NMax]; | ||
vector<pair<int,int>> G[NMax]; | ||
vector<int> distances; | ||
vector<pair<int,int>> edges; | ||
vector<int> costs; | ||
|
||
void bellman(int N, const vector<pair<int,int>> ad[NMax]) { | ||
distances.push_back(0); | ||
// Initialize costs from source to other nodes (neighbours and non-neighbours) | ||
// Non-neighbours | ||
for (int i = 1; i < N; i++) { | ||
distances.push_back(INF); | ||
} | ||
// Neighbours | ||
for(pair<int, int> neighbour: ad[0]) { | ||
distances[neighbour.first] = neighbour.second; | ||
} | ||
|
||
// Relax edges |E|*(|V|-1) times | ||
for(int i = 1; i <= N - 1; i++) { | ||
int j = 0; | ||
for(pair<int, int> edge: edges) { | ||
if(distances[edge.second] > distances[edge.first] + costs[j]){ | ||
distances[edge.second] = distances[edge.first] + costs[j]; | ||
} | ||
j++; | ||
} | ||
|
||
} | ||
|
||
int j = 0; | ||
// If we can relax one more time edges, then we have a cycle | ||
for(pair<int, int> edge: edges) { | ||
if(distances[edge.second] > distances[edge.first] + costs[j]){ | ||
cout << "Cycle!\n"; | ||
exit(-1); | ||
} | ||
j++; | ||
} | ||
|
||
} | ||
|
||
void Print( int N, const vector<pair<int,int>> ad[NMax], int* Parent ) { | ||
|
||
for (int i = 0; i < distances.size(); i++) { | ||
cout << distances[i] << " "; | ||
} | ||
|
||
} | ||
|
||
int main() { | ||
freopen("bellman.in", "r", stdin); | ||
freopen("bellman.out", "w", stdout); | ||
|
||
scanf("%d%d", &N, &M); | ||
|
||
while ( M -- ) { | ||
int x, y, cost; | ||
scanf("%d%d%d", &x, &y, &cost); | ||
G[x].push_back(make_pair(y, cost)); | ||
G[y].push_back(make_pair(x, cost)); | ||
edges.push_back(make_pair(x,y)); | ||
|
||
costs.push_back(cost); | ||
} | ||
|
||
bellman(N, G); | ||
|
||
Print(N, G, Parent); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
5 8 | ||
0 1 -1 | ||
0 2 4 | ||
1 2 3 | ||
1 3 2 | ||
1 4 2 | ||
3 2 5 | ||
3 1 1 | ||
4 3 -3 | ||
|
This file was deleted.
Oops, something went wrong.
File renamed without changes.
Oops, something went wrong.