Skip to content
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

Merged

Conversation

Kiran-Venkatesh
Copy link
Contributor

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.

@@ -63,7 +63,7 @@ def add_edge(self, source, target, cost=None):
GraphEdge(self.__getattribute__(source),
self.__getattribute__(target),
cost)

Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

@@ -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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use spaces after ','.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def minDistance(graph: Graph,dist,visited,source):
def min_distance(graph: Graph, dist, visited, source):

graph: Graph
The graph under consideration.
start: str
The name of the source the node.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The name of the source the node.
The name of the source node.

@@ -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
Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
min = sys.maxsize
min = float('inf')

@Kiran-Venkatesh
Copy link
Contributor Author

Kiran-Venkatesh commented Oct 7, 2020 via email


def minDistance(graph: Graph,dist,visited,source):
min = sys.maxsize
min_index=source
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
min_index=source
min_index = source

except:
pass

L={}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
L={}
L = {}

Comment on lines 928 to 945
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
Copy link
Member

@czgdp1807 czgdp1807 Oct 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:
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?

Copy link
Contributor Author

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?

Copy link
Member

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.

@Kiran-Venkatesh
Copy link
Contributor Author

Kiran-Venkatesh commented Oct 7, 2020 via email

dist[start] = 0

for cout in range(V):
u = minDistance(graph,dist,visited,start)
Copy link
Member

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).

Copy link
Contributor Author

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?

Copy link
Member

@czgdp1807 czgdp1807 Oct 7, 2020

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.

Copy link
Contributor Author

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

Copy link
Contributor Author

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?

Copy link
Member

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).

Copy link
Contributor Author

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?

Copy link
Contributor Author

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

@Kiran-Venkatesh
Copy link
Contributor Author

Kiran-Venkatesh commented Oct 7, 2020 via email

@czgdp1807
Copy link
Member

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"

@Kiran-Venkatesh
Copy link
Contributor Author

Kiran-Venkatesh commented Oct 7, 2020 via email

@Kiran-Venkatesh
Copy link
Contributor Author

removed trailing spaces and included binomial heap

@codecov
Copy link

codecov bot commented Oct 10, 2020

Codecov Report

Merging #301 into master will increase coverage by 0.027%.
The diff coverage is 100.000%.

@@              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               
Impacted Files Coverage Δ
pydatastructs/graphs/algorithms.py 99.450% <100.000%> (+0.042%) ⬆️
pydatastructs/linear_data_structures/__init__.py 100.000% <0.000%> (ø)
pydatastructs/linear_data_structures/algorithms.py 99.447% <0.000%> (+0.172%) ⬆️

Impacted file tree graph

@czgdp1807
Copy link
Member

Thanks for your contributions.

@czgdp1807 czgdp1807 merged commit 8877bf7 into codezonediitj:master Oct 10, 2020
@Kiran-Venkatesh
Copy link
Contributor Author

Is that my code got merged? I was unable to push my updated code for last 4 days?

@czgdp1807
Copy link
Member

I made corrective changes by pushing to your branch(edits from maintainers).

@Kiran-Venkatesh
Copy link
Contributor Author

Its gave some other contributer pushing my code

@czgdp1807
Copy link
Member

Yes.

@Kiran-Venkatesh
Copy link
Contributor Author

Kiran-Venkatesh commented Oct 10, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add Dijsktra Algorithm
3 participants