Skip to content

Commit

Permalink
added algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
techhunter2 committed Oct 8, 2022
1 parent 7b13472 commit 9a0293b
Show file tree
Hide file tree
Showing 23 changed files with 617 additions and 0 deletions.
Binary file added BFS-DFS/bfs.exe
Binary file not shown.
93 changes: 93 additions & 0 deletions BFS-DFS/bfs_dfs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include <iostream>
#include <vector>
#include <list>
#include <stack>

// Graph
class Graph
{
private:
std::vector<std::list<int>> adjList;
int vertices;

public:
Graph(int v)
{
this->vertices = v;
this->adjList.resize(v);
}

void addEdge(int source, int dest)
{
this->adjList[source].push_back(dest);
}

void bfs(int source)
{
std::list<int> queue;
std::vector<bool> visited;
visited.resize(this->vertices, false);
queue.push_back(source);
visited[source] = true;
std::cout << "\nBreadth First Search: ";
while (!queue.empty())
{
int curr = queue.front();
std::cout << " -> " << curr;
queue.pop_front();
for (auto vertex : this->adjList[curr])
{
if (!visited[vertex])
{
visited[vertex] = true;
queue.push_back(vertex);
}
}
}
}

void dfs(int source)
{
std::stack<int> s;
std::vector<bool> visited;
visited.resize(this->vertices, false);
s.push(source);
visited[source] = true;
std::cout << "\nDepth First Search: ";
while (!s.empty())
{
int curr = s.top();
std::cout << " -> " << curr;
s.pop();
for (auto vertex : this->adjList[curr])
{
if (!visited[vertex])
{
visited[vertex] = true;
s.push(vertex);
}
}
}
}
};

// main()
int main(int argc, char *argv[])
{
int vertices, edges, source, dest;
std::cout << "\nVertices & Egdes: ";
std::cin >> vertices >> edges;
std::cout << "Edges Source Destination: " << std::endl;

Graph graph(vertices);
for (int idx = 0; idx < edges; ++idx)
{
std::cin >> source >> dest;
graph.addEdge(source, dest);
}
std::cout << "\nSOurce: " << std::endl;
std::cin >> source;
graph.bfs(source);
graph.dfs(source);
return 0;
}
Binary file added BFS-DFS/bfs_dfs.exe
Binary file not shown.
90 changes: 90 additions & 0 deletions BellManFord/BellManFord.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include <stdio.h>
#include <stdlib.h>

#define INF 9999;

struct Edge
{
int u, v, w;
};

struct Graph
{
int vertices, edges;
struct Edge *edge;
};

struct Graph *initialize_Graph()
{
int vertices, edges;
struct Graph *G = (struct Graph *)malloc(sizeof(struct Graph));
printf("\n No. Of vertices and edges: ");
scanf("%d%d", &vertices, &edges);
G->vertices = vertices;
G->edges = edges;
G->edge = (struct Edge *)malloc(G->edges * sizeof(struct Edge));
for (int e = 0; e < edges; e++)
{
printf("\n Enter Edge %d info U V W: ", e);
scanf("%d%d%d", &G->edge[e].u, &G->edge[e].v, &G->edge[e].w);
}
for (int e = 0; e < edges; e++)
{
printf("\n Enter Edge %d info U=%d V=%d W=%d: ", e, G->edge[e].u, G->edge[e].v, G->edge[e].w);
}
return G;
}

int BellManFord(struct Graph *G, int source)
{
int distance[G->vertices], predesessor[G->edges];
for (int d = 0; d < G->vertices; d++)
{
distance[d] = INF;
predesessor[d] = -1;
}
distance[source] = 0;

for (int v = 0; v < G->vertices - 1; v++)
{
for (int e = 0; e < G->edges; e++)
{
if (distance[G->edge[e].v] > (distance[G->edge[e].u] + G->edge[e].w))
{
distance[G->edge[e].v] = distance[G->edge[e].u] + G->edge[e].w;
predesessor[G->edge[e].v] = G->edge[e].u;
}
}
}

for (int e = 0; e < G->edges - 1; e++)
{
if (distance[G->edge[e].v] > (distance[G->edge[e].u] + G->edge[e].w))
{
printf("Has Negetive Cycle!");
return -1;
}
}
printf("Distance Array D: | ");
for (int v = 0; v < G->vertices; v++)
{
printf(" %d |", distance[v]);
}
printf("\t Predecessor Array: | ");
for (int p = 0; p < G->vertices; p++)
{
printf(" %d |", predesessor[p]);
}
return 1;
}

// Main Function
int main()
{
struct Graph *g = initialize_Graph();
int source;
printf("\n Source: ");
scanf("%d", &source);
BellManFord(g, source);
return 0;
}
Binary file added BellManFord/BellManFord.exe
Binary file not shown.
57 changes: 57 additions & 0 deletions FloydWarshall/FloydWarshall.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <iostream>

#define INF 999;
int graph[100][100];

void showGraph(int vertices)
{

for (int s = 0; s < vertices; ++s)
{
for (int d = 0; d < vertices; ++d)
{
std::cout << graph[s][d] << " ";
}
std::cout << std::endl;
}
}

void floydWarshall(int vertices)
{
for (int via = 0; via < vertices; ++via)
{
for (int s = 0; s < vertices; ++s)
{
for (int d = 0; d < vertices; ++d)
{
if (graph[s][via] + graph[via][d] < graph[s][d])
{
graph[s][d] = graph[s][via] + graph[via][d];
}
}
}
}
}

int main()
{
int vertices;
std::cout << "\nNo Of Vertices: ";
std::cin >> vertices;
for (int s = 0; s < vertices; ++s)
{
for (int d = 0; d < vertices; ++d)
{
std::cout << "Enter Weight " << s << "->" << d << ":";
std::cin >> graph[s][d];
if (graph[s][d] == 0 && s != d)
{
graph[s][d] = INF;
}
}
}

floydWarshall(vertices);
showGraph(vertices);
return 0;
}
Binary file added FloydWarshall/FloydWarshall.exe
Binary file not shown.
82 changes: 82 additions & 0 deletions FractionalKnapsack/knapsack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include <iostream>

float fractionalKnapsack(float weight[], float profit[], float capacity, int n)
{
int i = 0;
float maxprofit = 0.0, fraction;
while (weight[i] <= capacity)
{
maxprofit = maxprofit + profit[i];
capacity = capacity - float(weight[i]);
i++;
}
if (i < n)
{
fraction = capacity / weight[i];
maxprofit = maxprofit + fraction * profit[i];
}

return maxprofit;
}

float fractionalKnapsack1(float weight[], float profit[], float capacity, int n)
{
float fraction;
if (n == 0)
{
return 0;
}

if (weight[n] > capacity)
{
fraction = capacity / weight[n];
return fraction * profit[n];
}
if (weight[n] <= capacity)
{
capacity = capacity - weight[n];
return profit[n] + fractionalKnapsack1(weight, profit, capacity, n - 1);
}
}

int main()
{
int n;
float capacity;
std::cout << "Items: ";
std::cin >> n;
std::cout << "\nCapacity: ";
std::cin >> capacity;
float profit[n], weight[n];
float ratio[n];
for (int item = 0; item < n; item++)
{
std::cout << "weight profit: ";
std::cin >> weight[item] >> profit[item];
ratio[item] = profit[item] / weight[item];
}

// sort by ratio
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (ratio[i] > ratio[j])
{
ratio[i] = ratio[i] + ratio[j];
ratio[j] = ratio[i] - ratio[j];
ratio[i] = ratio[i] - ratio[j];

profit[i] = profit[i] + profit[j];
profit[j] = profit[i] - profit[j];
profit[i] = profit[i] - profit[j];

weight[i] = weight[i] + weight[j];
weight[j] = weight[i] - weight[j];
weight[i] = weight[i] - weight[j];
}
}
}
std::cout << "Max Profit: " << fractionalKnapsack1(weight, profit, capacity, n);
return 0;
}
Binary file added FractionalKnapsack/knapsack.exe
Binary file not shown.
29 changes: 29 additions & 0 deletions KnapSack01/knapsack01.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <bits/stdc++.h>
using namespace std;

int knapSack(int capacity, int weight[], int profit[], int n)
{
if (n == 0 || capacity == 0)
return 0;
if (weight[n - 1] > capacity)
return knapSack(capacity, weight, profit, n - 1);
else
{
if (profit[n - 1] + knapSack(capacity - weight[n - 1], weight, profit, n - 1) > knapSack(capacity, weight, profit, n - 1))
{
return profit[n - 1] + knapSack(capacity - weight[n - 1], weight, profit, n - 1);
}
return knapSack(capacity, weight, profit, n - 1);
}
}

// Driver code
int main()
{
int profit[] = {60, 100, 120};
int weight[] = {10, 20, 30};
int capacity = 50;
int n = sizeof(profit) / sizeof(profit[0]);
cout << knapSack(capacity, weight, profit, n);
return 0;
}
Binary file added KnapSack01/knapsack01.exe
Binary file not shown.
11 changes: 11 additions & 0 deletions MatrixChainOrder/init.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include<iostream>

int MatrixChainOrder(int p[], int n){
int m[n][n];
int i, j, k;
return 1;
}

int main(){
return 0;
}
Binary file added MergeSort/22Mergesort.pdf
Binary file not shown.
Binary file added MergeSort/MIT6_006F11_lec03.pdf
Binary file not shown.
Binary file added MergeSort/ini.exe
Binary file not shown.
Loading

0 comments on commit 9a0293b

Please sign in to comment.