From b298213a4b02f50d5686e611f0c6e1f8aaa5aa3c Mon Sep 17 00:00:00 2001 From: AyushSingh-github <70777486+AyushSingh-github@users.noreply.github.com> Date: Fri, 1 Oct 2021 23:42:22 +0530 Subject: [PATCH 1/5] Add files via upload --- Python/BasicGraphs.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Python/BasicGraphs.py diff --git a/Python/BasicGraphs.py b/Python/BasicGraphs.py new file mode 100644 index 00000000..791b68c7 --- /dev/null +++ b/Python/BasicGraphs.py @@ -0,0 +1,38 @@ +class Graph: + + graph_dict={} + + def addEdge(self,node,neighbour): + if node not in self.graph_dict: + self.graph_dict[node]=[neighbour] + else: + self.graph_dict[node].append(neighbour) + + def show_edges(self): + for node in self.graph_dict: + for neighbour in self.graph_dict[node]: + print("(",node,", ",neighbour,")") +# paths between two nodes. + def find_path(self,start,end,path=[]): + path = path + [start] + if start==end: + return path + '''Loop for transversing all neighbouring nodes of the start node and then recursively calls itself again untill finds a path from one node to the end node. ''' + for node in self.graph_dict[start]: + if node not in path: + newPath=self.find_path(node,end,path) + if newPath: + return newPath + return None + +g= Graph() +g.addEdge('1', '2') +g.addEdge('1', '3') +g.addEdge('2', '3') +g.addEdge('2', '1') +g.addEdge('3', '1') +g.addEdge('3', '2') +g.addEdge('3', '4') +g.addEdge('4', '3') +g.show_edges() +print(g.find_path('4', '1')) \ No newline at end of file From 7306d38bb38d231d0c08fe98dd02e07b8c492ae4 Mon Sep 17 00:00:00 2001 From: AyushSingh-github <70777486+AyushSingh-github@users.noreply.github.com> Date: Fri, 1 Oct 2021 23:47:06 +0530 Subject: [PATCH 2/5] Add files via upload This is my Graph code for node printing and transversal --- Python/program-40/BasicGraphs.py | 38 ++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Python/program-40/BasicGraphs.py diff --git a/Python/program-40/BasicGraphs.py b/Python/program-40/BasicGraphs.py new file mode 100644 index 00000000..791b68c7 --- /dev/null +++ b/Python/program-40/BasicGraphs.py @@ -0,0 +1,38 @@ +class Graph: + + graph_dict={} + + def addEdge(self,node,neighbour): + if node not in self.graph_dict: + self.graph_dict[node]=[neighbour] + else: + self.graph_dict[node].append(neighbour) + + def show_edges(self): + for node in self.graph_dict: + for neighbour in self.graph_dict[node]: + print("(",node,", ",neighbour,")") +# paths between two nodes. + def find_path(self,start,end,path=[]): + path = path + [start] + if start==end: + return path + '''Loop for transversing all neighbouring nodes of the start node and then recursively calls itself again untill finds a path from one node to the end node. ''' + for node in self.graph_dict[start]: + if node not in path: + newPath=self.find_path(node,end,path) + if newPath: + return newPath + return None + +g= Graph() +g.addEdge('1', '2') +g.addEdge('1', '3') +g.addEdge('2', '3') +g.addEdge('2', '1') +g.addEdge('3', '1') +g.addEdge('3', '2') +g.addEdge('3', '4') +g.addEdge('4', '3') +g.show_edges() +print(g.find_path('4', '1')) \ No newline at end of file From 0fdaeb63a9145f0da69074dd5fa330f574274752 Mon Sep 17 00:00:00 2001 From: AyushSingh-github <70777486+AyushSingh-github@users.noreply.github.com> Date: Fri, 1 Oct 2021 23:48:20 +0530 Subject: [PATCH 3/5] Delete BasicGraphs.py --- Python/BasicGraphs.py | 38 -------------------------------------- 1 file changed, 38 deletions(-) delete mode 100644 Python/BasicGraphs.py diff --git a/Python/BasicGraphs.py b/Python/BasicGraphs.py deleted file mode 100644 index 791b68c7..00000000 --- a/Python/BasicGraphs.py +++ /dev/null @@ -1,38 +0,0 @@ -class Graph: - - graph_dict={} - - def addEdge(self,node,neighbour): - if node not in self.graph_dict: - self.graph_dict[node]=[neighbour] - else: - self.graph_dict[node].append(neighbour) - - def show_edges(self): - for node in self.graph_dict: - for neighbour in self.graph_dict[node]: - print("(",node,", ",neighbour,")") -# paths between two nodes. - def find_path(self,start,end,path=[]): - path = path + [start] - if start==end: - return path - '''Loop for transversing all neighbouring nodes of the start node and then recursively calls itself again untill finds a path from one node to the end node. ''' - for node in self.graph_dict[start]: - if node not in path: - newPath=self.find_path(node,end,path) - if newPath: - return newPath - return None - -g= Graph() -g.addEdge('1', '2') -g.addEdge('1', '3') -g.addEdge('2', '3') -g.addEdge('2', '1') -g.addEdge('3', '1') -g.addEdge('3', '2') -g.addEdge('3', '4') -g.addEdge('4', '3') -g.show_edges() -print(g.find_path('4', '1')) \ No newline at end of file From db7b08734cc56f2e9539d9138aa99639fcf8f1a3 Mon Sep 17 00:00:00 2001 From: AyushSingh-github <70777486+AyushSingh-github@users.noreply.github.com> Date: Fri, 1 Oct 2021 23:53:51 +0530 Subject: [PATCH 4/5] Update BasicGraphs.py --- Python/program-40/BasicGraphs.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/Python/program-40/BasicGraphs.py b/Python/program-40/BasicGraphs.py index 791b68c7..8a69cc21 100644 --- a/Python/program-40/BasicGraphs.py +++ b/Python/program-40/BasicGraphs.py @@ -1,3 +1,4 @@ +# the program below implements the above graph using two functions namely add_edge to add edges to the graph and show_graph to show all the edges in the graph. class Graph: graph_dict={} @@ -26,6 +27,7 @@ def find_path(self,start,end,path=[]): return None g= Graph() +#inputs for the graphs nodes. i took pre defined nodes for easy observation of adjacent nodes. g.addEdge('1', '2') g.addEdge('1', '3') g.addEdge('2', '3') @@ -35,4 +37,18 @@ def find_path(self,start,end,path=[]): g.addEdge('3', '4') g.addEdge('4', '3') g.show_edges() -print(g.find_path('4', '1')) \ No newline at end of file +print(g.find_path('4', '1')) + +''' +The output of the graph will be the same as the input we provided it - +( 1 , 2 ) +( 1 , 3 ) +( 2 , 3 ) +( 2 , 1 ) +( 3 , 1 ) +( 3 , 2 ) +( 3 , 4 ) +( 4 , 3 ) +#path of the nodes transversal +[‘4’, ‘3’, ‘1’] +''' From 48faca4071ca76d3b49f185fb642d539b276d1e5 Mon Sep 17 00:00:00 2001 From: AyushSingh-github <70777486+AyushSingh-github@users.noreply.github.com> Date: Sat, 2 Oct 2021 00:01:41 +0530 Subject: [PATCH 5/5] Create Readme.md.txt --- Python/program-40/Readme.md.txt | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Python/program-40/Readme.md.txt diff --git a/Python/program-40/Readme.md.txt b/Python/program-40/Readme.md.txt new file mode 100644 index 00000000..ba20cbf8 --- /dev/null +++ b/Python/program-40/Readme.md.txt @@ -0,0 +1,38 @@ +A graph is a pictorial representation of a set of objects where some pairs of objects are connected by links. The interconnected objects are represented by points termed as vertices, and the links that connect the vertices are called edges. + +From the above definition it is clear that graphs are a set of vertices that are connected using edges and can be directed or undirected. +So what are the best data structures that we can use to implement graphs in python. +They are none other than dictionaries and lists. +We would be using a combination of both to show a node and their neighbouring vertices. +For eg the representation of above graph would look something just like this - +{ + 1:[2,3], + 2:[1,3], + 3:[1,2,4], + 4:[3] +} +As simple as that. +Now coming on how to write a program to implement the same. +Well that’s also very easy the program below implements the above graph using two functions namely add_edge to add edges to the graph and show_graph to show all the edges in the graph + +Let’s find the path between two nodes +Just add this function to your existing class to find a path between any two nodes. + +And at the end of your file remove the last line and add this line - +print(g.find_path(‘4’, ‘1’)) +Finally after running the program you should get the following result - +[‘4’, ‘3’, ‘1’] +So what’s happening here, + +Let’s break the code into smaller pieces to understand it more properly. +So first things first, our function takes 3 arguments namely start, end and path. The first two variables, as the name suggests, store the start and end nodes. The third variable stores the current path while the function recursively calls itself to update that path. +Initially the path is set to an empty list and then we append the start node to it, after that we check if start==end , if yes then we return the path i.e. only the start node. +Then comes this - +for node in self.graph_dict[start]: + if node not in path: + newPath=self.find_path(node,end,path) + if newPath: + return newPath + return None +This loop traverses all the neighbouring nodes of the start node and then recursively calls itself again until it finds a path from one of the neighbouring nodes to end node, if it finds a path it returns the path. +And remember that it does not return the shortest, it just returns the first path it finds.