Skip to content

Commit

Permalink
Merge branch 'master' into ternary_search_javascript
Browse files Browse the repository at this point in the history
  • Loading branch information
thuva4 committed Oct 15, 2018
2 parents 65e4e92 + b9ba6e9 commit 15e272e
Show file tree
Hide file tree
Showing 290 changed files with 4,458 additions and 3,552 deletions.
35 changes: 35 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
@@ -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.
17 changes: 17 additions & 0 deletions .github/ISSUE_TEMPLATE/feature_request.md
@@ -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.
143 changes: 41 additions & 102 deletions README_BASE_7327.md → Algorithms.md

Large diffs are not rendered by default.

File renamed without changes.
Expand Up @@ -17,7 +17,8 @@ struct Graph
{
int V, E;
// V is number of vertices and E is number of edges


// list of all edges.
struct Edge* edge;
// This structure contain another structure which we already created edge.
};
Expand All @@ -41,9 +42,8 @@ void FinalSolution(int dist[], int n)
{
// This function prints the final solution
cout<<"\nVertex\tDistance from Source Vertex\n";
int i;

for (i = 0; i < n; ++i){
for (int i = 0; i < n; ++i){
cout<<i<<"\t\t"<<dist[i]<<"\n";
}
}
Expand All @@ -55,21 +55,21 @@ void BellmanFord(struct Graph* graph, int source)
int E = graph->E;

int StoreDistance[V];

int i,j;



// This is initial step that we know , we initialize all distance to infinity except source.
// We assign source distance as 0(zero)

for (i = 0; i < V; i++)
for (int i = 0; i < V; i++)
StoreDistance[i] = INT_MAX;

StoreDistance[source] = 0;

//The shortest path of graph that contain V vertices, never contain "V-1" edges. So we do here "V-1" relaxations
for (i = 1; i <= V-1; i++)
// The shortest path of graph that contain V vertices, never contain "V-1"
// edges. So we do here "V-1" relaxations
for (int i = 1; i <= V-1; i++)
{
for (j = 0; j < E; j++)
for (int j = 0; j < E; j++)
{
int u = graph->edge[j].source;

Expand All @@ -82,11 +82,12 @@ void BellmanFord(struct Graph* graph, int source)
}
}

// Actually upto now shortest path found. But BellmanFord checks for negative edge cycle. In this step we check for that
// Actually upto now shortest path found. But BellmanFord checks for
// negative edge cycle. In this step we check for that
// shortest distances if graph doesn't contain negative weight cycle.

// If we get a shorter path, then there is a negative edge cycle.
for (i = 0; i < E; i++)
for (int i = 0; i < E; i++)
{
int u = graph->edge[i].source;

Expand All @@ -99,8 +100,6 @@ void BellmanFord(struct Graph* graph, int source)
}

FinalSolution(StoreDistance, V);

return;
}

int main()
Expand All @@ -118,8 +117,7 @@ int main()

struct Graph* graph = createGraph(V, E); //calling the function to allocate space to these many vertices and edges

int i;
for(i=0;i<E;i++){
for(int i=0;i < E; i++){
cout<<"\nEnter edge "<<i+1<<" properties Source, destination, weight respectively\n";
cin>>graph->edge[i].source;
cin>>graph->edge[i].destination;
Expand Down
File renamed without changes.
File renamed without changes.
110 changes: 110 additions & 0 deletions BellmanFord/C#/BellmanFord.cs
@@ -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;
}
}
}
84 changes: 84 additions & 0 deletions BellmanFord/C++/BellmanFord.cpp
@@ -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;
}
10 changes: 10 additions & 0 deletions BellmanFord/C++/bellman.in
@@ -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

29 changes: 0 additions & 29 deletions BellmanFord/C/bellman-ford.c

This file was deleted.

File renamed without changes.

0 comments on commit 15e272e

Please sign in to comment.