Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
File renamed without changes.
3 changes: 3 additions & 0 deletions Divide and Conquer/linkedlist.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

// TODO: Implement linked list
49 changes: 49 additions & 0 deletions Divide and Conquer/singlyLinkedList.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include<bits/stdc++.h>
using namespace std;
// creating class of node
class Node
{
public:
int data;
Node* next;
//constructor
Node(int x)
{
data=x;
next=NULL;
}
};
void traverse(Node* head)
{
if(head==NULL)
return;
cout<<head->data<<" ";
traverse(head->next);
}
int main() {
Node* head,*temp,*temp1;
head=NULL;
int n;
// number of nodes
cin>>n;
for(int i=0;i<n;i++)
{
int x;
cin>>x;
if(head==NULL)
{
head=new Node(x);
temp=head;
}
else
{
temp1=new Node(x);
temp->next=temp1;
temp=temp1;
}
}
//traverse the linked list
traverse(head);
cout<<"\n";
return 0;
}
Binary file added Dynamic Programming/Level-2/.DS_Store
Binary file not shown.
71 changes: 71 additions & 0 deletions Graphs/C++/graph.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#pragma once

/**
* Author : Shanborlang Suja
* NIT Meghalaya
*/

// Generic Graph data structure works for both directed and non directed graph
// Helps in adding vertices and edges, and can return the list of vertices and the neighbours of a specified vertex

#include <map>
#include <vector>
#include <stdexcept>
#include <numeric>

template<typename Vertex = int, typename Weight = double>
class Graph {
public:
using vertex_type = Vertex;
using weight_type = Weight;
using neighbor_type = std::pair<Vertex, Weight>;
using neighbor_list_type = std::vector<neighbor_type>;

void add_edge(Vertex const source, Vertex const target, Weight const weight, bool const bidirectional = true) {
adjacency_list[source].push_back(std::make_pair(target, weight));
if(bidirectional) adjacency_list[target].push_back(std::make_pair(source, weight)); // for non directed graph
}

size_t vertex_count() const { return adjacency_list.size(); }

std::vector<Vertex> vertices() const {
std::vector<Vertex> keys;
for(auto const& [first, second] : adjacency_list)
keys.push_back(first);
return keys;
}

neighbor_list_type const& neighbors(Vertex const& v) const {
auto pos = adjacency_list.find(v);
if(pos == adjacency_list.end())
throw std::runtime_error("vertex not found");
return pos->second;
}

constexpr static Weight INF = std::numeric_limits<Weight>::infinity(); // This is basically max size of the type of Weight

private:
std::map<vertex_type, neighbor_list_type> adjacency_list;
};

/**
* Usage:
*
* Graph<int, int> graph; --> Create a graph data structure
*
*
* // Add value to the graph
* graph.add_edge(0, 1, 4);
* graph.add_edge(1, 7, 11);
* graph.add_edge(0, 7, 8);
* graph.add_edge(1, 2, 8);
* graph.add_edge(2, 3, 7);
* graph.add_edge(2, 8, 2);
* graph.add_edge(8, 6, 6);
* graph.add_edge(7, 8, 7);
* graph.add_edge(2, 5, 4);
* graph.add_edge(6, 5, 2);
* graph.add_edge(3, 5, 14);
* graph.add_edge(3, 4, 9);
* graph.add_edge(5, 4, 10);
*/
103 changes: 103 additions & 0 deletions Graphs/C++/shortest_path.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#pragma once

/**
* Author : Shanborlang Suja
* NIT Meghalaya
*/

/**
* The below shortest_path() function is implemented using Dijkstra algorithm
*
* Pseudocode:
*
* function Dijkstra(Graph, source):
* dist[source] <- 0 // Initialization
* create vertex set Q
* for each vertex v in Graph
* if v != source
* dist[v] <- INF // Unknown distance from source to v
* prev[v] <- UNDEFINED // Predecessor of v
* Q.add_with_priority(v, dist[v])
*
* while Q is not empty: // The main loop
* u <- Q.extract_min() // Remove and return best vertex
* for each neighbor v of u: // only v that is still in Q
* alt <- dist[u] + length(u, v)
* if alt < dist[u]
* dist[v] <- alt
* prev[v] <- u
* Q.decrease_priority(v, alt)
*
* return dist[], prev[]
*
*
* The above pseudocode is refer from https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm
*/

#include "graph.hpp"

#include <set>
#include <algorithm>
#include <iostream>

template<typename Vertex, typename Weight>
void shortest_path(Graph<Vertex, Weight> const& g, Vertex const source, std::map<Vertex, Weight>& min_distance, std::map<Vertex, Vertex>& previous) {
auto const n = g.vertex_count(); // Get the number of vertex
auto const vertices = g.vertices(); // Get the vertices

min_distance.clear();
for(auto const& v : vertices) {
min_distance[v] = Graph<Vertex, Weight>::INF;
}
min_distance[source] = 0;

previous.clear();

std::set<std::pair<Weight, Vertex>> vertex_queue;
vertex_queue.insert(std::make_pair(min_distance[source], source));

while(!vertex_queue.empty()) {
auto [dist, u] = *(vertex_queue.begin());

vertex_queue.erase(std::begin(vertex_queue));

auto const& neighbors = g.neighbors(u);
for(auto const& [v, w] : neighbors) {
auto dist_via_u = dist + w;
if(dist_via_u < min_distance[v]) {
vertex_queue.erase(std::make_pair(min_distance[v], v));

min_distance[v] = dist_via_u;
previous[v] = u;
vertex_queue.insert(std::make_pair(min_distance[v], v));
}
}
}
}

// Some helper functions
template<typename Vertex>
void build_path(std::map<Vertex, Vertex> const& prev, Vertex const v, std::vector<Vertex>& result) {
result.push_back(v);

auto pos = prev.find(v);
if(pos == std::end(prev)) return;

build_path(prev, pos->second, result);
}

template<typename Vertex>
std::vector<Vertex> build_path(std::map<Vertex, Vertex> const& prev, Vertex const v) {
std::vector<Vertex> result;
build_path(prev, v, result);
std::reverse(std::begin(result), std::end(result));
return result;
}

template<typename Vertex>
void print_path(std::vector<Vertex> const& path) {
for(size_t i = 0; i < path.size(); i++) {
std::cout << path[i];
if(i < path.size() - 1) std::cout << " -> ";
}
}
66 changes: 66 additions & 0 deletions Graphs/C++/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include<iostream>

#include "graph.hpp"
#include "shortest_path.hpp"

int main() {

// TEST 1
Graph<char, double> g;
std::cout << "TEST 1\n";
g.add_edge('A', 'B', 7);
g.add_edge('A', 'C', 9);
g.add_edge('A', 'F', 14);
g.add_edge('B', 'C', 10);
g.add_edge('B', 'D', 15);
g.add_edge('C', 'D', 11);
g.add_edge('C', 'F', 2);
g.add_edge('D', 'E', 6);
g.add_edge('E', 'F', 9);

char source = 'A';
std::map<char, double> min_distance;
std::map<char, char> previous;
shortest_path(g, source, min_distance, previous);

for (auto const &[first, second] : min_distance)
{
std::cout << source << " -> " << first << " : " << second << '\t';

print_path(build_path(previous, first));

std::cout << std::endl;
}


// TEST 2
Graph<char, double> graph;
graph.add_edge('0', '1', 4);
graph.add_edge('1', '7', 11);
graph.add_edge('0', '7', 8);
graph.add_edge('1', '2', 8);
graph.add_edge('2', '3', 7);
graph.add_edge('2', '8', 2);
graph.add_edge('8', '6', 6);
graph.add_edge('7', '8', 7);
graph.add_edge('2', '5', 4);
graph.add_edge('6', '5', 2);
graph.add_edge('3', '5', 14);
graph.add_edge('3', '4', 9);
graph.add_edge('5', '4', 10);
graph.add_edge('7', '6', 1);

char src = '0';
std::map<char, double> min_dist;
std::map<char, char> prev;
shortest_path(graph, src, min_dist, prev);
std::cout << "\nTEST 2" << std::endl;
for (auto const &[first, second] : min_dist)
{
std::cout << src << " -> " << first << " : " << second << '\t';

print_path(build_path(prev, first));

std::cout << std::endl;
}
}
Empty file removed Graphs/Initial.md
Empty file.
27 changes: 27 additions & 0 deletions Graphs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Graphs Alogrithm

---

## **Problem 1**: Shortest path between nodes
Given a network of nodes and the distances between **them**, computes and display the shortest
distance from a specified node to all **others**, as well as the path between the **start** and end **node**.

### **Example**
<p>Consider the following graph as input:[TEST 2]</p>
<img src="https://www.geeksforgeeks.org/wp-content/uploads/Fig-11.jpg" alt="Picture" width="200" height="100"/>

### Output :
<h4> Format : <bold>origin -> destination : cost&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;path</bold></h4>
<pre>
0 -> 0 : 0 0
0 -> 2 : 12 0 -> 1 -> 2
0 -> 3 : 19 0 -> 1 -> 2 -> 3
0 -> 4 : 21 0 -> 7 -> 6 -> 5 -> 4
0 -> 5 : 11 0 -> 7 -> 6 -> 5
0 -> 6 : 9 0 -> 7 -> 6
0 -> 7 : 8 0 -> 7
0 -> 8 : 14 0 -> 1 -> 2 -> 8
</pre>

### Solution : `graph.hpp`, `shortest_path.hpp` and `test.cpp`

Binary file modified Linked Lists/.DS_Store
Binary file not shown.
Binary file added Priority Queue and Heaps/.DS_Store
Binary file not shown.