From 2288576d14c5ba235199268259d5978d285095d0 Mon Sep 17 00:00:00 2001 From: Mayank Singh Date: Wed, 14 Oct 2020 18:15:00 +0530 Subject: [PATCH 1/9] added new file --- .../{ Question => Question.md } | 0 .../{ Solution => Solution.c++} | 0 Dynamic Programming/Level-2/.DS_Store | Bin 0 -> 6148 bytes 3 files changed, 0 insertions(+), 0 deletions(-) rename Dynamic Programming/Level-2/ Longest_palindromic_substring/{ Question => Question.md } (100%) rename Dynamic Programming/Level-2/ Longest_palindromic_substring/{ Solution => Solution.c++} (100%) create mode 100644 Dynamic Programming/Level-2/.DS_Store diff --git a/Dynamic Programming/Level-2/ Longest_palindromic_substring/ Question b/Dynamic Programming/Level-2/ Longest_palindromic_substring/ Question.md similarity index 100% rename from Dynamic Programming/Level-2/ Longest_palindromic_substring/ Question rename to Dynamic Programming/Level-2/ Longest_palindromic_substring/ Question.md diff --git a/Dynamic Programming/Level-2/ Longest_palindromic_substring/ Solution b/Dynamic Programming/Level-2/ Longest_palindromic_substring/ Solution.c++ similarity index 100% rename from Dynamic Programming/Level-2/ Longest_palindromic_substring/ Solution rename to Dynamic Programming/Level-2/ Longest_palindromic_substring/ Solution.c++ diff --git a/Dynamic Programming/Level-2/.DS_Store b/Dynamic Programming/Level-2/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..4ede20e32d6a2b4dcd3993dde91dec5d30ba70d0 GIT binary patch literal 6148 zcmeHKL2DC16n>LNJBf%n6xx%_MeqvHjgkEu z_8<6D{006T{oc%2T2oI25jzjwd~asnd&_)pcV>r(#BiSM5_O13LKzz^G*<}Ev#v?Q zjWmJ6UXZ4oF4ixij8cpxRABA|_7Un@(wt&bYYc>0~kPY(3X`6{Y#4Fu`dufy=u$X`blCP|x$EGM zO@m*(XTURX1p~Z4C@5p#u(oKn4m9Qp0Oa3DBe3~5kYkj?z+r6>GZ0}zfi_gQD~7P) z7+20Oa9CTk;UwJUL%1gkcS8~8>Bz5)JBh%eZ#@H^fy)eR*lvf<|ARl@|1Ssmo@c-_ z@Lw??n)~7Y5KD4r>)PV@tX0rMC=2`57SBUqn4=iJd=&3MjbL1{0Sp}07BK?xKLUmZ L-*^W8Dg!?ODkO*Z literal 0 HcmV?d00001 From 644818d361b166f1787e4f6d9159f8d281a07d0f Mon Sep 17 00:00:00 2001 From: Mayank Singh Date: Wed, 14 Oct 2020 18:19:29 +0530 Subject: [PATCH 2/9] added new file --- .../{ Question.md => problem.md } | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Dynamic Programming/Level-2/ Longest_palindromic_substring/{ Question.md => problem.md } (100%) 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 From acd71b8fc545fd7c20dc57eca1f6caf6e3439b45 Mon Sep 17 00:00:00 2001 From: Shanborlang Date: Sat, 17 Oct 2020 04:11:40 +0530 Subject: [PATCH 3/9] Implemented shortest path problem in graphs --- Graphs/C++/graph.hpp | 71 ++++++++++++++++++++++++ Graphs/C++/shortest_path.hpp | 103 +++++++++++++++++++++++++++++++++++ Graphs/C++/test.cpp | 66 ++++++++++++++++++++++ Graphs/Initial.md | 27 +++++++++ 4 files changed, 267 insertions(+) create mode 100644 Graphs/C++/graph.hpp create mode 100644 Graphs/C++/shortest_path.hpp create mode 100644 Graphs/C++/test.cpp diff --git a/Graphs/C++/graph.hpp b/Graphs/C++/graph.hpp new file mode 100644 index 0000000..da32363 --- /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 DAG and non DAG +// 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 DAG + } + + 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 index e69de29..1df019f 100644 --- a/Graphs/Initial.md +++ b/Graphs/Initial.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` + From d99651ba2904fcc32a48dd666ae5c3d172846f43 Mon Sep 17 00:00:00 2001 From: Shanborlang Date: Sat, 17 Oct 2020 04:19:12 +0530 Subject: [PATCH 4/9] Renaming Initial.md to README.md in graph folder for convenience --- Basics+1/Sorting/{Initial.md => README.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Basics+1/Sorting/{Initial.md => README.md} (100%) 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 From 9cb374dbb47bab39f00f09c4d9140810f3d10ecd Mon Sep 17 00:00:00 2001 From: Shanborlang Date: Sat, 17 Oct 2020 04:20:45 +0530 Subject: [PATCH 5/9] Renaming Initial.md to README.md in graph folder for convenience --- Graphs/{Initial.md => README.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Graphs/{Initial.md => README.md} (100%) diff --git a/Graphs/Initial.md b/Graphs/README.md similarity index 100% rename from Graphs/Initial.md rename to Graphs/README.md From ec176369b44f9e1c9c52574efe89f776605c7e13 Mon Sep 17 00:00:00 2001 From: Shanborlang Date: Sat, 17 Oct 2020 04:24:16 +0530 Subject: [PATCH 6/9] fixing typo in graph.hpp --- Graphs/C++/graph.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Graphs/C++/graph.hpp b/Graphs/C++/graph.hpp index da32363..fb8286d 100644 --- a/Graphs/C++/graph.hpp +++ b/Graphs/C++/graph.hpp @@ -5,7 +5,7 @@ * NIT Meghalaya */ -// Generic Graph data structure works for both DAG and non DAG +// 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 From 37e2c427bf5a04ceb06632a0eb72230c1fa17f9b Mon Sep 17 00:00:00 2001 From: Shanborlang Date: Sat, 17 Oct 2020 04:24:58 +0530 Subject: [PATCH 7/9] fixing typo in graph.hpp --- Graphs/C++/graph.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Graphs/C++/graph.hpp b/Graphs/C++/graph.hpp index fb8286d..e260db4 100644 --- a/Graphs/C++/graph.hpp +++ b/Graphs/C++/graph.hpp @@ -23,7 +23,7 @@ class Graph { 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 DAG + 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(); } From ed5845761de2e29caa62cf07425bb29ac041f6ee Mon Sep 17 00:00:00 2001 From: Mayank Singh Date: Sat, 17 Oct 2020 13:33:03 +0530 Subject: [PATCH 8/9] changingSomething --- .DS_Store | Bin 8196 -> 10244 bytes Priority Queue and Heaps/.DS_Store | Bin 0 -> 6148 bytes 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 Priority Queue and Heaps/.DS_Store diff --git a/.DS_Store b/.DS_Store index c8c5cbfef1a854e2bf4c5a4875aabd59411d627e..9fb0e989df8462b5a2468d9a93bcce545054a3ab 100644 GIT binary patch delta 229 zcmZp1XbF&DU|?W$DortDU{C-uIe-{M3-C-V6q~50$SAWhU^hRb%;Z=>gUvPq)-1ei z4DJj?42cW{3>gf?lWm1>NTP_81sCPzi|Y=RIuAi>d>N=QT~C;<)#$qI1D4atZeN(FWu6-&m85{IY_LH>>Q z5BL8G@Zy>V1V|n412H!Yu$8e|88+)^}&i=2uDSu!6onVU38fygS^?iSEX`g zW_C{NnyxL>J_qM!6y#w(9CX6|C+Z!B`FX_lkum9$9Y)7VZ**E;+A~=mCRwjJ=!cR1Kc~?O{OHK9_JrrGoE@Y5Ejv7J=>u>?_A4Kh7-0wtT zS40>P284mv%>W;Vdj564ro5dnAPoEm2Ize7Pzim;!l6Anu(2foBK<~MLEHZPBYNaO z^cf3>=s{5~7154$7tDxX#v*Dn8htdiI!oV~G zW!J6K{eSTH`hPk}dcuG(@U9q8mHlA9gG=JO_1WU+uJusQP)R7RaQKvhhP{e0magIz bR4eHB$UyWN3y0`Ikskp|gEYdxKV{$-8n0yO literal 0 HcmV?d00001 From af086083a3a28fa97ee5bd81aa54eb1e164a24b3 Mon Sep 17 00:00:00 2001 From: Mayank Singh Date: Tue, 20 Oct 2020 17:26:52 +0530 Subject: [PATCH 9/9] adding file --- .DS_Store | Bin 10244 -> 10244 bytes Divide and Conquer/linkedlist.h | 3 ++ Divide and Conquer/singlyLinkedList.c++ | 49 ++++++++++++++++++++++++ Linked Lists/.DS_Store | Bin 6148 -> 6148 bytes 4 files changed, 52 insertions(+) create mode 100644 Divide and Conquer/linkedlist.h create mode 100644 Divide and Conquer/singlyLinkedList.c++ diff --git a/.DS_Store b/.DS_Store index 9fb0e989df8462b5a2468d9a93bcce545054a3ab..2c5d117fba26f21a858bae0e735c8e57cb0e8544 100644 GIT binary patch delta 63 zcmZn(XbG6$&nUMsU^hRb+~in6gUvPqQS7&o64 R;^f)PuJDUx^95mMW&oUa6h8m} delta 50 zcmV-20L}k|P=rvBPXQdUP`eKS9FtuUFta=mlme4S6eqJN6yFQ6fH48H2O# +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/Linked Lists/.DS_Store b/Linked Lists/.DS_Store index a6ac3027553ced4104bdd460d0c17ddbd28e314c..220c81251dbf562319531adb54c53c65217223f5 100644 GIT binary patch literal 6148 zcmeHK!Ab)$5Phiyt5v9==rKp3Q1I-*rM8DcPx=GeZq>zY3tENV_ToSI0bca?`~ZEE zgtFVNAP6Ed1Cy6z=Vg+YWwQZb+7Iy&&;U@Q3syE*l$bmhU$IK)StlAd#wq3)BE<-; zm1ynwiwelzZQ}|FCWw*Wzag4v_R{HakWL4Tl2_!Fe)!>xHDD7x@)FGGm)!Rj#)y|V z$BaA4aq5hdY`1(%Vp!L@$E?UQ`())aZ53ED0y)fgQ^I@BW!vG566T4?AEE2YF>4dY zE?J{_TFShKGsZvC@0fFhes13wOWUB=4FuQ`kP_s78t$9c=GiA^{$N1yq5e0&Bk6kn_L$_5Hs{ z(v~Wq3j8YtOto{~X>&>8Y+YHLoVAvIK^K#_%;SQ>iaUxKD@XAG-5L9>RESZ)%p-ef P`Xk_E&_WgXRRul)wx@Y5 delta 92 zcmZoMXfc=|#>CJzu~2NHo}wrt0|NsP3otNbG9)tOFz7MlGNeo_RA*$I?8aoi`53b! r%jOHr%*>nFIruq%+BP3#{?0s^UqqJ^r1Jm}GfcMOk=`65vV<7`w5k-e