Skip to content

Commit

Permalink
standardization
Browse files Browse the repository at this point in the history
  • Loading branch information
jainaman224 committed Jan 3, 2017
1 parent c436565 commit 36e0996
Show file tree
Hide file tree
Showing 63 changed files with 445 additions and 445 deletions.
4 changes: 2 additions & 2 deletions Fenwick_Tree/Fenwick_Tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ int lowbit(int x) {
}

void update(int index, int value) {
// add "value" to the index-th element
// add "value" to the index-th element
for( ; index <= N ; index += lowbit(index)) {
bit[index] += value;
}
Expand All @@ -26,7 +26,7 @@ int query(int index) {
}

int main(){

N = 10;

// WARNING: the index of the first element is 1
Expand Down
7 changes: 4 additions & 3 deletions Fermat_Little_Theorem/Fermat_Little_Theorem.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

int power(int a, unsigned int n, int p)
{
int res = 1;
a = a % p;

while (n>0)
{
if (n & 1)
res = (res*a)%p;
n = n>>1;
a = (a*a) % p;
}

return res;
}

Expand All @@ -31,7 +32,7 @@ bool isPrime(unsigned int n, int k)
return false;
k--;
}

return true;
}

Expand Down
24 changes: 14 additions & 10 deletions Floyd_Warshall_Algorithm/Floyd_Warshall_Algorithm.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
#include <iostream>
#include <limits.h>
#include <iomanip>

#define Infinity INT_MAX
#define A 4

using namespace std;

void FloydWarshall(int graph[A][A]);

void output(int length[A][A]);


void FloydWarshall(int graph[A][A])
{ int length[A][A],x,y,z;
{
int length[A][A],x,y,z;
for(x = 0; x < A; x++)
for(y = 0; y < A; y++)
length[x][y] = graph[x][y];



for(z = 0; z < A; z++)
for(x = 0; x < A; x++)
for(y = 0; y < A; y++)
Expand All @@ -26,19 +27,22 @@ void FloydWarshall(int graph[A][A])
length[x][y] = length[x][z] + length[z][y];
}
output(length);
}
}

void output(int length[A][A])
{ cout << "The matrix below shows the shortest distances between each pair of vertices\n";
{
cout << "The matrix below shows the shortest distances between each pair of vertices\n";
for (int x = 0; x < A; x++)
{for (int y = 0; y < A; y++)
{ if (length[x][y] == Infinity)
{
for (int y = 0; y < A; y++)
{
if (length[x][y] == Infinity)
cout << setw(12) << "INFINITY";
else
cout << setw(12) << length[x][y];
}
cout << endl;
}
}
}

int main() {
Expand All @@ -47,7 +51,7 @@ int main() {
{Infinity, Infinity, 0, 7},
{Infinity, Infinity, Infinity, 0}
};
FloydWarshall(graph);
FloydWarshall(graph);
return 0;
}

Expand Down
4 changes: 2 additions & 2 deletions Floyd_Warshall_Algorithm/Floyd_Warshall_Algorithm.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package main

import (
"fmt"
"math"
Expand Down Expand Up @@ -31,7 +31,7 @@ func floyd_warshall(graph [][]float64) [][]float64 {
}
return dist
}

func main() {
graph := [][]float64{
{0, 5, math.Inf(1), 10},
Expand Down
2 changes: 1 addition & 1 deletion Floyd_Warshall_Algorithm/Floyd_Warshall_Algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def main():
[float('inf'), float('inf'), 0, 1],
[float('inf'), float('inf'), float('inf'), 0]]


floyd = FloydWarshall(graph)
floyd.run()
floyd.print_distance()
Expand Down
183 changes: 92 additions & 91 deletions Ford_Fulkerson_Method/Ford_Fulkerson_Method.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,110 +3,111 @@
#include <limits.h>
#include <string.h>
#include <queue>

using namespace std;
 

// Number of vertices in given graph
#define V 6
 

/* Returns true if there is a path from source 's' to sink 't' in
  residual graph. Also fills parent[] to store the path */
bool bfs(int rGraph[V][V], int s, int t, int parent[])
{
    // Create a visited array and mark all vertices as not visited
    bool visited[V];
    memset(visited, 0, sizeof(visited));
 
    // Create a queue, enqueue source vertex and mark source vertex
    // as visited
    queue <int> q;
    q.push(s);
    visited[s] = true;
    parent[s] = -1;
 
    // Standard BFS Loop
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
 
        for (int v=0; v<V; v++)
        {
            if (visited[v]==false && rGraph[u][v] > 0)
            {
                q.push(v);
                parent[v] = u;
                visited[v] = true;
            }
        }
    }
 
    // If we reached sink in BFS starting from source, then return
    // true, else false
    return (visited[t] == true);
// Create a visited array and mark all vertices as not visited
bool visited[V];
memset(visited, 0, sizeof(visited));

// Create a queue, enqueue source vertex and mark source vertex
// as visited
queue <int> q;
q.push(s);
visited[s] = true;
parent[s] = -1;

// Standard BFS Loop
while (!q.empty())
{
int u = q.front();
q.pop();

for (int v = 0; v < V; v++)
{
if (visited[v]==false && rGraph[u][v] > 0)
{
q.push(v);
parent[v] = u;
visited[v] = true;
}
}
}

// If we reached sink in BFS starting from source, then return
// true, else false
return (visited[t] == true);
}
 

// Returns tne maximum flow from s to t in the given graph
int fordFulkerson(int graph[V][V], int s, int t)
{
    int u, v;
 
    // Create a residual graph and fill the residual graph with
    // given capacities in the original graph as residual capacities
    // in residual graph
    int rGraph[V][V]; // Residual graph where rGraph[i][j] indicates
                     // residual capacity of edge from i to j (if there
                     // is an edge. If rGraph[i][j] is 0, then there is not) 
    for (u = 0; u < V; u++)
        for (v = 0; v < V; v++)
             rGraph[u][v] = graph[u][v];
 
    int parent[V];  // This array is filled by BFS and to store path
 
    int max_flow = 0;  // There is no flow initially
 
    // Augment the flow while tere is path from source to sink
    while (bfs(rGraph, s, t, parent))
    {
        // Find minimum residual capacity of the edhes along the
        // path filled by BFS. Or we can say find the maximum flow
        // through the path found.
        int path_flow = INT_MAX;
        for (v=t; v!=s; v=parent[v])
        {
            u = parent[v];
            path_flow = min(path_flow, rGraph[u][v]);
        }
 
        // update residual capacities of the edges and reverse edges
        // along the path
        for (v=t; v != s; v=parent[v])
        {
            u = parent[v];
            rGraph[u][v] -= path_flow;
            rGraph[v][u] += path_flow;
        }
 
        // Add path flow to overall flow
        max_flow += path_flow;
    }
 
    // Return the overall flow
    return max_flow;
int u, v;

// Create a residual graph and fill the residual graph with
// given capacities in the original graph as residual capacities
// in residual graph
int rGraph[V][V]; // Residual graph where rGraph[i][j] indicates
// residual capacity of edge from i to j (if there
// is an edge. If rGraph[i][j] is 0, then there is not) 
for (u = 0; u < V; u++)
for (v = 0; v < V; v++)
rGraph[u][v] = graph[u][v];

int parent[V];//This array is filled by BFS and to store path

int max_flow = 0;//There is no flow initially

// Augment the flow while tere is path from source to sink
while (bfs(rGraph, s, t, parent))
{
// Find minimum residual capacity of the edhes along the
// path filled by BFS. Or we can say find the maximum flow
// through the path found.
int path_flow = INT_MAX;
for (v=t; v!=s; v=parent[v])
{
u = parent[v];
path_flow = min(path_flow, rGraph[u][v]);
}

// update residual capacities of the edges and reverse edges
// along the path
for (v=t; v != s; v=parent[v])
{
u = parent[v];
rGraph[u][v] -= path_flow;
rGraph[v][u] += path_flow;
}

// Add path flow to overall flow
max_flow += path_flow;
}

// Return the overall flow
return max_flow;
}
 

// Driver program to test above functions
int main()
{
    // Let us create a graph shown in the above example
    int graph[V][V] = { {0, 16, 13, 0, 0, 0},
                        {0, 0, 10, 12, 0, 0},
                        {0, 4, 0, 0, 14, 0},
                        {0, 0, 9, 0, 0, 20},
                        {0, 0, 0, 7, 0, 4},
                        {0, 0, 0, 0, 0, 0}
                      };
 
    cout << "The maximum possible flow is " << fordFulkerson(graph, 0, 5);
 
    return 0;
// Let us create a graph shown in the above example
int graph[V][V] = { {0, 16, 13, 0, 0, 0},
{0, 0, 10, 12, 0, 0},
{0, 4, 0, 0, 14, 0},
{0, 0, 9, 0, 0, 20},
{0, 0, 0, 7, 0, 4},
{0, 0, 0, 0, 0, 0}
};

cout << "The maximum possible flow is " << fordFulkerson(graph, 0, 5);

return 0;
}
8 changes: 4 additions & 4 deletions Ford_Fulkerson_Method/Ford_Fulkerson_Method.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@


class Ford_Fulkerson_Method {

//Number of vertices in graph
static final int V = 6;

/* Returns true if there is a path from source 's' to sink
't' in residual graph. Also fills parent[] to store the
path */
Expand Down Expand Up @@ -118,7 +118,7 @@ public static void main (String[] args) throws java.lang.Exception
m.fordFulkerson(graph, 0, 5));

}


}
/* Output
Expand All @@ -127,4 +127,4 @@ public static void main (String[] args) throws java.lang.Exception
*/
*/
Loading

0 comments on commit 36e0996

Please sign in to comment.