From 285720f76d03ddffbe6fc28bfa97a381eeda50c8 Mon Sep 17 00:00:00 2001 From: Francisco Matias Date: Thu, 22 Jun 2017 18:53:33 -0300 Subject: [PATCH 1/2] Graph, Deep First Search, Graph with Matrix, Graph with List --- Graphs/Deep_First_Search.py | 35 +++++++++++++++++++++++++++++++++++ Graphs/Graph_list.py | 27 +++++++++++++++++++++++++++ Graphs/Graph_matrix.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 Graphs/Deep_First_Search.py create mode 100644 Graphs/Graph_list.py create mode 100644 Graphs/Graph_matrix.py diff --git a/Graphs/Deep_First_Search.py b/Graphs/Deep_First_Search.py new file mode 100644 index 000000000..d34601003 --- /dev/null +++ b/Graphs/Deep_First_Search.py @@ -0,0 +1,35 @@ +class Graph: + + def __init__(self, vertex): + self.vertex = vertex + self.graph = [[0] * vertex for i in range(vertex) ] + self.visited = [False] * vertex + + def add_edge(self, u, v): + self.graph[u - 1][v - 1] = 1 + self.graph[v - 1][u - 1] = 1 + + def show(self): + + for i in self.graph: + for j in i: + print(j, end=' ') + print(' ') + + + def dfs(self, u): + self.visited[u - 1] = True + print('%d visited' % u) + for i in range(1, self.vertex + 1): + if self.graph[u - 1][i - 1] == 1 and self.visited[i - 1] == False: + self.dfs(i) + + +g = Graph(5) + +g.add_edge(1,4) +g.add_edge(4,2) +g.add_edge(4,5) +g.add_edge(2,5) +g.add_edge(5,3) +print(g.dfs(1)) diff --git a/Graphs/Graph_list.py b/Graphs/Graph_list.py new file mode 100644 index 000000000..cb380bb31 --- /dev/null +++ b/Graphs/Graph_list.py @@ -0,0 +1,27 @@ +class Graph: + def __init__(self, vertex): + self.vertex = vertex + self.graph = [[0] for i in range(vertex)] + + def add_edge(self, u, v): + self.graph[u - 1].append(v - 1) + + def show(self): + for i in range(self.vertex): + print('%d: '% (i + 1), end=' ') + for j in self.graph[i]: + print('%d-> '% (j + 1), end=' ') + print(' ') + + + +g = Graph(5) + +g.add_edge(1,3) +g.add_edge(2,3) +g.add_edge(3,4) +g.add_edge(3,5) +g.add_edge(4,5) + +g.show() + diff --git a/Graphs/Graph_matrix.py b/Graphs/Graph_matrix.py new file mode 100644 index 000000000..0141d2be8 --- /dev/null +++ b/Graphs/Graph_matrix.py @@ -0,0 +1,30 @@ +class Graph: + + def __init__(self, vertex): + self.vertex = vertex + self.graph = [[0] * vertex for i in range(vertex) ] + + def add_edge(self, u, v): + self.graph[u - 1][v - 1] = 1 + self.graph[v - 1][u - 1] = 1 + + def show(self): + + for i in self.graph: + for j in i: + print(j, end=' ') + print(' ') + + + + +g = Graph(5) + +g.add_edge(1,3) +g.add_edge(2,3) +g.add_edge(3,4) +g.add_edge(3,5) +g.add_edge(4,5) + +g.show() + From 494e7f55ec68a994684d256559c3e444c9f7b263 Mon Sep 17 00:00:00 2001 From: Francisco Matias Date: Thu, 22 Jun 2017 20:15:03 -0300 Subject: [PATCH 2/2] Breadth_First_Search --- Graphs/Breadth_First_Search.py | 45 ++++++++++++++++++++++++++++++++++ Graphs/Deep_First_Search.py | 4 +-- 2 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 Graphs/Breadth_First_Search.py diff --git a/Graphs/Breadth_First_Search.py b/Graphs/Breadth_First_Search.py new file mode 100644 index 000000000..f3d9fd778 --- /dev/null +++ b/Graphs/Breadth_First_Search.py @@ -0,0 +1,45 @@ +class Graph: + + def __init__(self, vertex): + self.vertex = vertex + self.graph = [[0] * vertex for i in range(vertex) ] + + def add_edge(self, u, v): + self.graph[u - 1][v - 1] = 1 + self.graph[v - 1][u - 1] = 1 + + def show(self): + + for i in self.graph: + for j in i: + print(j, end=' ') + print(' ') + def bfs(self,v): + + visited = [False]*self.vertex + visited[v - 1] = True + print('%d visited' % (v)) + + queue = [v - 1] + while len(queue) > 0: + v = queue[0] + for u in range(self.vertex): + if self.graph[v][u] == 1: + if visited[u]== False: + visited[u] = True + queue.append(u) + print('%d visited' % (u +1)) + queue.pop(0) + +g = Graph(10) + +g.add_edge(1,2) +g.add_edge(1,3) +g.add_edge(1,4) +g.add_edge(2,5) +g.add_edge(3,6) +g.add_edge(3,7) +g.add_edge(4,8) +g.add_edge(5,9) +g.add_edge(6,10) +g.bfs(1) diff --git a/Graphs/Deep_First_Search.py b/Graphs/Deep_First_Search.py index d34601003..51c0eb6b8 100644 --- a/Graphs/Deep_First_Search.py +++ b/Graphs/Deep_First_Search.py @@ -8,7 +8,6 @@ class Graph: def add_edge(self, u, v): self.graph[u - 1][v - 1] = 1 self.graph[v - 1][u - 1] = 1 - def show(self): for i in self.graph: @@ -26,10 +25,9 @@ class Graph: g = Graph(5) - g.add_edge(1,4) g.add_edge(4,2) g.add_edge(4,5) g.add_edge(2,5) g.add_edge(5,3) -print(g.dfs(1)) +g.dfs(1)