diff --git a/.DS_Store b/.DS_Store index c8c5cbf..2c5d117 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/Basics+1/Sorting/Initial.md b/Basics+1/Sorting/README.md similarity index 100% rename from Basics+1/Sorting/Initial.md rename to Basics+1/Sorting/README.md diff --git a/Divide and Conquer/linkedlist.h b/Divide and Conquer/linkedlist.h new file mode 100644 index 0000000..5fc6429 --- /dev/null +++ b/Divide and Conquer/linkedlist.h @@ -0,0 +1,3 @@ +#pragma once + +// TODO: Implement linked list diff --git a/Divide and Conquer/singlyLinkedList.c++ b/Divide and Conquer/singlyLinkedList.c++ new file mode 100644 index 0000000..ae08b57 --- /dev/null +++ b/Divide and Conquer/singlyLinkedList.c++ @@ -0,0 +1,49 @@ +#include +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<data<<" "; + traverse(head->next); +} +int main() { + Node* head,*temp,*temp1; + head=NULL; + int n; + // number of nodes + cin>>n; + for(int i=0;i>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; +} diff --git a/Dynamic Programming/Level-2/ Longest_palindromic_substring/longestPalindromicSubstring.cpp b/Dynamic Programming/Level-2/ Longest_palindromic_substring/ Solution.c++ similarity index 100% rename from Dynamic Programming/Level-2/ Longest_palindromic_substring/longestPalindromicSubstring.cpp rename to Dynamic Programming/Level-2/ Longest_palindromic_substring/ Solution.c++ diff --git a/Dynamic Programming/Level-2/ Longest_palindromic_substring/Question.md b/Dynamic Programming/Level-2/ Longest_palindromic_substring/ problem.md similarity index 100% rename from Dynamic Programming/Level-2/ Longest_palindromic_substring/Question.md rename to Dynamic Programming/Level-2/ Longest_palindromic_substring/ problem.md diff --git a/Dynamic Programming/Level-2/.DS_Store b/Dynamic Programming/Level-2/.DS_Store new file mode 100644 index 0000000..4ede20e Binary files /dev/null and b/Dynamic Programming/Level-2/.DS_Store differ diff --git a/Graphs/C++/graph.hpp b/Graphs/C++/graph.hpp new file mode 100644 index 0000000..e260db4 --- /dev/null +++ b/Graphs/C++/graph.hpp @@ -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 +#include +#include +#include + +template +class Graph { +public: + using vertex_type = Vertex; + using weight_type = Weight; + using neighbor_type = std::pair; + using neighbor_list_type = std::vector; + + 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 vertices() const { + std::vector 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::infinity(); // This is basically max size of the type of Weight + +private: + std::map adjacency_list; +}; + +/** + * Usage: + * + * Graph 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); +*/ \ No newline at end of file diff --git a/Graphs/C++/shortest_path.hpp b/Graphs/C++/shortest_path.hpp new file mode 100644 index 0000000..04cd924 --- /dev/null +++ b/Graphs/C++/shortest_path.hpp @@ -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 +#include +#include + +template +void shortest_path(Graph const& g, Vertex const source, std::map& min_distance, std::map& 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::INF; + } + min_distance[source] = 0; + + previous.clear(); + + std::set> 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 +void build_path(std::map const& prev, Vertex const v, std::vector& result) { + result.push_back(v); + + auto pos = prev.find(v); + if(pos == std::end(prev)) return; + + build_path(prev, pos->second, result); +} + +template +std::vector build_path(std::map const& prev, Vertex const v) { + std::vector result; + build_path(prev, v, result); + std::reverse(std::begin(result), std::end(result)); + return result; +} + +template +void print_path(std::vector const& path) { + for(size_t i = 0; i < path.size(); i++) { + std::cout << path[i]; + if(i < path.size() - 1) std::cout << " -> "; + } +} \ No newline at end of file diff --git a/Graphs/C++/test.cpp b/Graphs/C++/test.cpp new file mode 100644 index 0000000..cb82e46 --- /dev/null +++ b/Graphs/C++/test.cpp @@ -0,0 +1,66 @@ +#include + +#include "graph.hpp" +#include "shortest_path.hpp" + +int main() { + + // TEST 1 + Graph 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 min_distance; + std::map 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 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 min_dist; + std::map 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; + } +} \ No newline at end of file diff --git a/Graphs/Initial.md b/Graphs/Initial.md deleted file mode 100644 index e69de29..0000000 diff --git a/Graphs/README.md b/Graphs/README.md new file mode 100644 index 0000000..1df019f --- /dev/null +++ b/Graphs/README.md @@ -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** +

Consider the following graph as input:[TEST 2]

+Picture + +### Output : +

Format : origin -> destination : cost      path

+
+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
+
+ +### Solution : `graph.hpp`, `shortest_path.hpp` and `test.cpp` + diff --git a/Linked Lists/.DS_Store b/Linked Lists/.DS_Store index a6ac302..220c812 100644 Binary files a/Linked Lists/.DS_Store and b/Linked Lists/.DS_Store differ diff --git a/Priority Queue and Heaps/.DS_Store b/Priority Queue and Heaps/.DS_Store new file mode 100644 index 0000000..4f7fe06 Binary files /dev/null and b/Priority Queue and Heaps/.DS_Store differ