-
Notifications
You must be signed in to change notification settings - Fork 273
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adding dijsktra algorithm #301
adding dijsktra algorithm #301
Conversation
@@ -63,7 +63,7 @@ def add_edge(self, source, target, cost=None): | |||
GraphEdge(self.__getattribute__(source), | |||
self.__getattribute__(target), | |||
cost) | |||
|
|||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The build is failing because of code quality.
Just remove the extra spaces here and use newline only.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pydatastructs/graphs/algorithms.py
Outdated
@@ -877,3 +879,73 @@ def _job(graph: Graph, u: str): | |||
if len(L) != num_vertices: | |||
raise ValueError("Graph is not acyclic.") | |||
return L | |||
|
|||
def minDistance(graph: Graph,dist,visited,source): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use spaces after ','.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def minDistance(graph: Graph,dist,visited,source): | |
def min_distance(graph: Graph, dist, visited, source): |
pydatastructs/graphs/algorithms.py
Outdated
graph: Graph | ||
The graph under consideration. | ||
start: str | ||
The name of the source the node. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name of the source the node. | |
The name of the source node. |
pydatastructs/graphs/algorithms.py
Outdated
@@ -877,3 +879,73 @@ def _job(graph: Graph, u: str): | |||
if len(L) != num_vertices: | |||
raise ValueError("Graph is not acyclic.") | |||
return L | |||
|
|||
def minDistance(graph: Graph,dist,visited,source): | |||
min = sys.maxsize |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think float('inf')
is better alternative for sys.maxsize
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
min = sys.maxsize | |
min = float('inf') |
Thanks for the feedback
…On Wed, Oct 7, 2020, 10:59 AM Prashant Rawat ***@***.***> wrote:
***@***.**** requested changes on this pull request.
------------------------------
In pydatastructs/graphs/adjacency_matrix.py
<#301 (comment)>
:
> @@ -63,7 +63,7 @@ def add_edge(self, source, target, cost=None):
GraphEdge(self.__getattribute__(source),
self.__getattribute__(target),
cost)
-
+
The build is failing because of code quality.
Just remove the extra spaces here and use newline only.
------------------------------
In pydatastructs/graphs/algorithms.py
<#301 (comment)>
:
> @@ -877,3 +879,73 @@ def _job(graph: Graph, u: str):
if len(L) != num_vertices:
raise ValueError("Graph is not acyclic.")
return L
+
+def minDistance(graph: Graph,dist,visited,source):
Use spaces after ','.
------------------------------
In pydatastructs/graphs/algorithms.py
<#301 (comment)>
:
> + if dist[v] < min and visited[v] is False:
+ min = dist[v]
+ min_index = v
+ return min_index
+
+def dijkstra_algorithm(graph: Graph,start: str):
+ """
+ Finds shortest path distance in the given graph from a given source to all vertex.
+
+ Parameters
+ ==========
+
+ graph: Graph
+ The graph under consideration.
+ start: str
+ The name of the source the node.
⬇️ Suggested change
- The name of the source the node.
+ The name of the source node.
------------------------------
In pydatastructs/graphs/algorithms.py
<#301 (comment)>
:
> @@ -877,3 +879,73 @@ def _job(graph: Graph, u: str):
if len(L) != num_vertices:
raise ValueError("Graph is not acyclic.")
return L
+
+def minDistance(graph: Graph,dist,visited,source):
+ min = sys.maxsize
I think float('inf') is better alternative for sys.maxsize.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#301 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AIUEACDZEQ3KNHRN7LT4AGTSJP4E5ANCNFSM4SGJTJDQ>
.
|
pydatastructs/graphs/algorithms.py
Outdated
|
||
def minDistance(graph: Graph,dist,visited,source): | ||
min = sys.maxsize | ||
min_index=source |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
min_index=source | |
min_index = source |
pydatastructs/graphs/algorithms.py
Outdated
except: | ||
pass | ||
|
||
L={} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
L={} | |
L = {} |
pydatastructs/graphs/algorithms.py
Outdated
V=len(graph.vertices) | ||
visited={} | ||
dist={} | ||
for v in graph.vertices: | ||
visited[v]=False | ||
if v!=start: | ||
dist[v] =sys.maxsize | ||
dist[start] = 0 | ||
|
||
for cout in range(V): | ||
u = minDistance(graph,dist,visited,start) | ||
visited[u] = True | ||
for v in graph.vertices: | ||
try: | ||
if graph.edge_weights[u+"_"+v].value> 0 and visited[v] is False and dist[v] > dist[u] + graph.edge_weights[u+"_"+v].value: | ||
dist[v] = dist[u] + graph.edge_weights[u+"_"+v].value | ||
except: | ||
pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
V=len(graph.vertices) | |
visited={} | |
dist={} | |
for v in graph.vertices: | |
visited[v]=False | |
if v!=start: | |
dist[v] =sys.maxsize | |
dist[start] = 0 | |
for cout in range(V): | |
u = minDistance(graph,dist,visited,start) | |
visited[u] = True | |
for v in graph.vertices: | |
try: | |
if graph.edge_weights[u+"_"+v].value> 0 and visited[v] is False and dist[v] > dist[u] + graph.edge_weights[u+"_"+v].value: | |
dist[v] = dist[u] + graph.edge_weights[u+"_"+v].value | |
except: | |
pass | |
V = len(graph.vertices) | |
visited = {} | |
dist = {} | |
for v in graph.vertices: | |
visited[v] = False | |
if v != start: | |
dist[v] = float('inf') | |
dist[start] = 0 | |
for cout in range(V): | |
u = min_distance(graph, dist, visited, start) | |
visited[u] = True | |
for v in graph.vertices: | |
edge_str = u + '_' + v | |
if (edge_str in graph.edge_weights and graph.edge_weights[edge_str].value > 0 and | |
visited[v] is False and dist[v] > dist[u] + graph.edge_weights[edge_str].value): | |
dist[v] = dist[u] + graph.edge_weights[edge_str].value |
Why we need try-except
block here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this what meant to do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this what meant to do?
This probably does what you wanted to do but without try-except
. try-except
shouldn't be used usually.
Try except is needed when edge_weights takes unknown vertex gives error
…On Wed, Oct 7, 2020, 2:13 PM Gagandeep Singh ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In pydatastructs/graphs/algorithms.py
<#301 (comment)>
:
> + V=len(graph.vertices)
+ visited={}
+ dist={}
+ for v in graph.vertices:
+ visited[v]=False
+ if v!=start:
+ dist[v] =sys.maxsize
+ dist[start] = 0
+
+ for cout in range(V):
+ u = minDistance(graph,dist,visited,start)
+ visited[u] = True
+ for v in graph.vertices:
+ try:
+ if graph.edge_weights[u+"_"+v].value> 0 and visited[v] is False and dist[v] > dist[u] + graph.edge_weights[u+"_"+v].value:
+ dist[v] = dist[u] + graph.edge_weights[u+"_"+v].value
+ except:
+ pass
⬇️ Suggested change
- V=len(graph.vertices)
- visited={}
- dist={}
- for v in graph.vertices:
- visited[v]=False
- if v!=start:
- dist[v] =sys.maxsize
- dist[start] = 0
-
- for cout in range(V):
- u = minDistance(graph,dist,visited,start)
- visited[u] = True
- for v in graph.vertices:
- try:
- if graph.edge_weights[u+"_"+v].value> 0 and visited[v] is False and dist[v] > dist[u] + graph.edge_weights[u+"_"+v].value:
- dist[v] = dist[u] + graph.edge_weights[u+"_"+v].value
- except:
- pass
+ V = len(graph.vertices)
+ visited = {}
+ dist = {}
+ for v in graph.vertices:
+ visited[v] = False
+ if v != start:
+ dist[v] = float('inf')
+ dist[start] = 0
+
+ for cout in range(V):
+ u = min_distance(graph, dist, visited, start)
+ visited[u] = True
+ for v in graph.vertices:
+ try:
+ if graph.edge_weights[u+"_"+v].value > 0 and visited[v] is False and dist[v] > dist[u] + graph.edge_weights[u+"_"+v].value:
+ dist[v] = dist[u] + graph.edge_weights[u+"_"+v].value
+ except:
+ pass
Why we need try-except block here?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#301 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AIUEACHOQ4NJWUCRSZ33U3LSJQS2DANCNFSM4SGJTJDQ>
.
|
pydatastructs/graphs/algorithms.py
Outdated
dist[start] = 0 | ||
|
||
for cout in range(V): | ||
u = minDistance(graph,dist,visited,start) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we can use Fibonacci heap(see heaps in this repo). Right Now complexity will be O(V^2). After Fibonacci heap, it should be O(VlogV).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In pydatastructs/trees/heaps.py directory,there is no fibonacci heaps implementation,then whether have to remove min_distance function and include implementation of fibonacci heap in pydatastructs/graphs/algorithms.py.
or otherwise i will push this existing one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use BinomialHeap
in that case. What we need is a priority queue.
class PriorityQueue(object): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
priority queue is it k to use in pydatastructs/pydatastructs/miscellaneous_data_structures/queue.py? to find the vertex which has minimum distance
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or binomial heap is fine to use for finding vertex position which has minimum distance?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest to use 'binomial_heap' implementation of PriorityQueue
(read its docs).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but inserting updated distances in binomial heap for all vertex takes O(V) time,then wats the use for O(logV) in this case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
included priorityqueue ,getting output in spyder but not here in CI/CD pipeline
Have to change initial part of Dijkstra algorithm?
…On Wed, Oct 7, 2020, 2:27 PM Gagandeep Singh ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In pydatastructs/graphs/algorithms.py
<#301 (comment)>
:
> + References
+ ==========
+
+ .. [1] https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm#:~:text=Dijkstra's
+ """
+ V=len(graph.vertices)
+ visited={}
+ dist={}
+ for v in graph.vertices:
+ visited[v]=False
+ if v!=start:
+ dist[v] =sys.maxsize
+ dist[start] = 0
+
+ for cout in range(V):
+ u = minDistance(graph,dist,visited,start)
Here we can use Fibonacci heap(see heaps in this repo). Right Now
complexity will be O(V^2). After Fibonacci heap, it should be O(VlogV).
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#301 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AIUEACHP54ZC7WLJSTGWLFLSJQUPFANCNFSM4SGJTJDQ>
.
|
There are quite a lot of lines with trailing white spaces. Can you please remove those spaces? For spyder the following should help, (Preferences -> Editor -> Advanced settings:) "Automatically remove trailing spaces when saving files" |
In Spyder,it works only when absolute path of libraries given, not relative
path in pydatastructs/graphs/init_.py
…On Thu, Oct 8, 2020, 12:28 AM Gagandeep Singh ***@***.***> wrote:
There are quite a lot of lines with trailing white spaces. Can you please
remove those spaces? For spyder the following should help, (Preferences ->
Editor -> Advanced settings:) "Automatically remove trailing spaces when
saving files"
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#301 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AIUEACA257ZLPKWYLEIUGL3SJS25DANCNFSM4SGJTJDQ>
.
|
removed trailing spaces and included binomial heap |
Codecov Report
@@ Coverage Diff @@
## master #301 +/- ##
=============================================
+ Coverage 98.827% 98.855% +0.027%
=============================================
Files 25 25
Lines 2901 2970 +69
=============================================
+ Hits 2867 2936 +69
Misses 34 34
|
Thanks for your contributions. |
Is that my code got merged? I was unable to push my updated code for last 4 days? |
I made corrective changes by pushing to your branch(edits from maintainers). |
Its gave some other contributer pushing my code |
Yes. |
Okay thanks for guidance and support
…On Sun, Oct 11, 2020, 1:08 AM Gagandeep Singh ***@***.***> wrote:
Yes.
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#301 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AIUEACHSFSFEMGBDWDKCEG3SKCZ47ANCNFSM4SGJTJDQ>
.
|
Fixes #153
Brief description of what is fixed or changed
I implemented the dijsktra algorithm . Added minDistance and dijkstra_algorithm function in pydatastructs.graphs.algorithms
Added test_dijkstra_algorithm and _test_dijkstra_algorithm in pydatastructs.graphs.tests.test_algorithms. I checked all export statements and init files to make sure it's working and also added system library for defining infinity value.
Other comments
I had a problem with relative directory in my python environment initially,but since it worked completely in this repo, I didn't change that. I'm making a PR for this first time.