Skip to content

Commit

Permalink
Try - Sqashing Commits
Browse files Browse the repository at this point in the history
  • Loading branch information
heisastark committed Apr 2, 2020
1 parent 2e9f2ac commit fdcff52
Show file tree
Hide file tree
Showing 3 changed files with 708 additions and 0 deletions.
110 changes: 110 additions & 0 deletions Boyer_Moore_Algorithm/Boyer_Moore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// C# Program for Boyer Moore String Matching Algorithm

using System;

public class Algorithm
{
static int CHARACTERS = 256;

// Getting maximum of two integers
static int max (int a, int b) { return (a > b)? a: b; }

// Bad Character Pre-Processing Function
static void badChar( char []str, int size,int []badCharacter)
{
int i;

// Initializing all occurences to -1
for (i = 0; i < CHARACTERS; i++)
badCharacter[i] = -1;

// Filling the Actual Value
for (i = 0; i < size; i++)
badCharacter[(int) str[i]] = i;
}

// Pattern Searching Function
static void search( char []txt, char []pat)
{
int m = pat.Length;
int n = txt.Length;

int []Character = new int[CHARACTERS];
badChar(pat, m, Character);

/*
s is used to keep track of
pattern shifting with respect to text
*/

int s = 0;

while(s <= (n - m))
{
int j = m - 1;

while(j >= 0 && pat[j] == txt[s+j])
j--;

/*
If the pattern is present at current
shift, then index j will become -1 after
the above loop
*/

if (j < 0)
{
Console.WriteLine("Pattern occurs at index: " + s);
s += (s+m < n)? m-Character[txt[s+m]] : 1;
}

else
s += max(1, j - Character[txt[s+j]]);

}
}

public static void Main()
{
Console.WriteLine("Enter The String Value: ");
String valueEntered = Console.ReadLine();
Console.WriteLine("Enter The Pattern To Search: ");
String pattern = Console.ReadLine();
Console.WriteLine();

char []txt = valueEntered.ToCharArray();
char []pat = pattern.ToCharArray();
search(txt, pat);
}
}

/**
Enter The String Value:
ABAAABCD
Enter The Pattern To Search:
ABC
Pattern occurs at index: 4
--------------------------------------------------
Enter The String Value:
AABAACAADAABAABA
Enter The Pattern To Search:
AABA
Pattern occurs at index: 0
Pattern occurs at index: 9
Pattern occurs at index: 12
--------------------------------------------------
Enter The String Value:
THIS IS A TEST TEXT
Enter The Pattern To Search:
TEST
Pattern occurs at index: 10
*/
180 changes: 180 additions & 0 deletions Johnson_Algorithm/Johnson_Algorithm.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/*
Johnson’s algorithm for All-pairs shortest paths
Given a weighted Directed Graph where the weights may be negative,
find the shortest path between every pair of vertices in the Graph using
Johnson’s Algorithm.
*/

int MAX_INT = 9223372036854775807;

int minDistance(distance, visited)
{
var minimum = MAX_INT;
var minVertex = 0;

var v = distance.length;

for(var vertex = 0; vertex < v; vertex++)
{
if( (minimum > distance[vertex]) && (visited[vertex] == false) )
{
minimum = distance[vertex];
minVertex = vertex;
}
}
return minVertex;
}

void Dijkstra(graph, modified, src)
{
var num_vertices = graph.length;
var distance = new List(num_vertices);

var visited = new List(num_vertices);

for (var i = 0; i < num_vertices; i++)
{
distance[i] = MAX_INT;
visited[i] = false;
}

distance[src] = 0;

for(var count = 0; count < num_vertices; count++)
{
var curVertex = minDistance(distance, visited);
visited[curVertex] = true;
for(var vertex = 0; vertex < num_vertices; vertex++)
{
if ((visited[vertex] == false) && (distance[vertex] > (distance[curVertex] +
modified[curVertex][vertex])) && (graph[curVertex][vertex] != 0))
{
distance[vertex] = (distance[curVertex] + modified[curVertex][vertex]);
}
}
}

for(var vertex = 0; vertex < num_vertices; vertex++)
{
print('Vertex ${vertex} : ${distance[vertex]}');
}
}

List BellmanFord(edges, graph, num_vertices)
{
var distance = new List(num_vertices+1);

for(var i = 0; i <= num_vertices; i++)
{
distance[i]=MAX_INT;
}

distance[num_vertices] = 0;

for(var i = 0; i < num_vertices; i++)
{
edges.add([num_vertices, i, 0]);
}

for(var i = 0; i < num_vertices; i++)
{
for(var j in edges)
{

if((distance[j[0]] != MAX_INT) && (distance[j[0]] + j[2] < distance[j[1]]) )
{
distance[j[1]] = distance[j[0]] + j[2];
}
}
}

return distance;
}


void JohnsonAlgorithm(graph)
{
var edges = new List();

for(var i = 0; i < graph.length ; i++)
{
for(var j = 0; j < graph[i].length; j++)
{
if(graph[i][j] != 0)
{
edges.add([i, j, graph[i][j]]);
}
}
}

var modifiedwei = BellmanFord(edges, graph, graph.length);

var modified = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0],
[0,0,0,0]];

for(var i = 0; i < graph.length; i++)
{
for(var j = 0; j < graph[i].length; j++)
{
if(graph[i][j] != 0)
{
modified[i][j] = (graph[i][j] +
modifiedwei[i] - modifiedwei[j]);
}
}
}

print ('Modified Graph: ${modified}');

for(var src = 0; src < graph.length; src++)
{
print ('\nShortest Distance with vertex ${src} as the source:\n');
Dijkstra(graph, modified, src);
}
}

void main() {

var graph = [[0, -8, 2, 4],
[0, 0, 2, 6],
[0, 0, 0, 2],
[0, 0, 0, 0]];

JohnsonAlgorithm(graph);
}

/*
Modified Graph: [[0, 0, 8, 8], [0, 0, 0, 2], [0, 0, 0, 0], [0, 0, 0, 0]]
Shortest Distance with vertex 0 as the source:
Vertex 0 : 0
Vertex 1 : 0
Vertex 2 : 0
Vertex 3 : 0
Shortest Distance with vertex 1 as the source:
Vertex 0 : 9223372036854775807
Vertex 1 : 0
Vertex 2 : 0
Vertex 3 : 0
Shortest Distance with vertex 2 as the source:
Vertex 0 : 9223372036854775807
Vertex 1 : 9223372036854775807
Vertex 2 : 0
Vertex 3 : 0
Shortest Distance with vertex 3 as the source:
Vertex 0 : 9223372036854775807
Vertex 1 : 9223372036854775807
Vertex 2 : -9223372036854775801
Vertex 3 : 0
*/
418 changes: 418 additions & 0 deletions Machine_Learning/Logistic_Regression/Logistic_Regression.ipynb

Large diffs are not rendered by default.

0 comments on commit fdcff52

Please sign in to comment.