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

K-Shortest path query fix #5410

Merged
merged 22 commits into from
May 15, 2020
Merged

Conversation

all-seeing-code
Copy link
Contributor

@all-seeing-code all-seeing-code commented May 9, 2020

Motivation

This PR fixes:

  • DGRAPH-794 -- Depth parameter has an off-by-one bug and in certain cases all paths are not returned even if depth is set to maximum.
  • DGRAPH-1371 -- Cyclical paths are not removed from the output.

For detailed discussion please refer here.

Components affected by this PR

K-shortest path queries with k > 1

Does this PR break backwards compatibility?

No. But it is a breaking fix in a sense that because of bug, code wasn't filtering out cyclical paths in all cases. This PR ensures that cyclical paths are filtered out. Hence code-output can change in some cases.

Testing

Added following tests in query3_test.go:

  1. For numpaths>1 check if correct paths are returned for different values of depth parameter. Also test that the paths returned become constant after a certain depth when the graph becomes saturated.
  2. Check that cyclical paths are not returned.

Fixes

Fixes DGRAPH-794
Fixes DGRAPH-1371


This change is Reviewable

Docs Preview: Dgraph Preview

Copy link
Contributor

@gja gja left a comment

Choose a reason for hiding this comment

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

Reviewable status: 0 of 1 files reviewed, 1 unresolved discussion (waiting on @anurags92 and @pawanrawal)


query/shortest.go, line 337 at r1 (raw file):

			newRoute := item.path
			newRoute.totalWeight = item.cost
			kroutes = append(kroutes, newRoute)

if len(newRoute) <= depth ? <- To be discussed

Copy link
Contributor

@ashish-goswami ashish-goswami left a comment

Choose a reason for hiding this comment

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

Reviewable status: 0 of 1 files reviewed, 3 unresolved discussions (waiting on @anurags92 and @pawanrawal)


query/shortest.go, line 73 at r2 (raw file):

	sort.Slice(tmp, func(i, j int) bool {
		return tmp[i].uid < tmp[j].uid
	})

Slice sorting and the searching is expensive here. Just a linear search would work fine.


query/shortest.go, line 383 at r2 (raw file):

		default:
			if len(kroutes) == numPaths {
				break

This will break out of switch case and not for loop. Use a labelled for loop here.

Copy link
Contributor

@pawanrawal pawanrawal left a comment

Choose a reason for hiding this comment

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

Reviewed 2 of 2 files at r3.
Reviewable status: all files reviewed, 6 unresolved discussions (waiting on @anurags92)


query/query3_test.go, line 683 at r3 (raw file):

	query := `
	{
		A as shortest(from: 51, to:55, numpaths: 5) {

Seems like the output of this one and the test below is the same. Could you give this query various inputs like numpaths 5, 6, 10, 100 etc. and see it still returns the same result.

Could you have a test which checks this for various values of depth as well along with a fixed value of numpaths say 5. Have cases for depth 0, 1, 2, 3, 4, 5, 10 and 100.


query/shortest.go, line 378 at r3 (raw file):

			return nil, ctx.Err()
		default:
			if len(kroutes) == numPaths {

Is this useful here? We already check this when we append to kroutes above. Does anything fail if you remove this?


query/shortest.go, line 389 at r3 (raw file):

				continue
			}
			if len(*item.path.route) > 0 && item.path.indexOf(toUid) != -1 {

This needs a comment on what it does and why is it needed. Also mention about the associated test case here.

Copy link
Contributor

@ashish-goswami ashish-goswami left a comment

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, 6 unresolved discussions (waiting on @anurags92)


query/shortest.go, line 66 at r3 (raw file):

func (r *route) indexOf(uid uint64) int {

You can pass a pointer to route that could avoid unnecessary copy.

query/query3_test.go Outdated Show resolved Hide resolved
Copy link
Contributor Author

@all-seeing-code all-seeing-code left a comment

Choose a reason for hiding this comment

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

Reviewable status: 0 of 3 files reviewed, 8 unresolved discussions (waiting on @ashish-goswami, @gja, and @pawanrawal)


query/query3_test.go, line 683 at r3 (raw file):

Previously, pawanrawal (Pawan Rawal) wrote…

Seems like the output of this one and the test below is the same. Could you give this query various inputs like numpaths 5, 6, 10, 100 etc. and see it still returns the same result.

Could you have a test which checks this for various values of depth as well along with a fixed value of numpaths say 5. Have cases for depth 0, 1, 2, 3, 4, 5, 10 and 100.

Addressed. Also added the tests which were earlier commented out. They work fine as well.


query/query3_test.go, line 593 at r4 (raw file):

Previously, gja (Tejas Dinkar) wrote…

can these be compressed a bit to not use so much vertical space
me: [
{"name": "x"},
{"name": "y"}
..
]

Yes, thanks. I have manually tried to minimize vertical spaces. But except for the name key, other params are nested. So could achieve only partial benefits.


query/shortest.go, line 337 at r1 (raw file):

Previously, gja (Tejas Dinkar) wrote…

if len(newRoute) <= depth ? <- To be discussed

As discussed in the discuss post, continuing with the current implementation of depth.


query/shortest.go, line 73 at r2 (raw file):

Previously, ashish-goswami (Ashish Goswami) wrote…

Slice sorting and the searching is expensive here. Just a linear search would work fine.

Done.


query/shortest.go, line 383 at r2 (raw file):

Previously, ashish-goswami (Ashish Goswami) wrote…

This will break out of switch case and not for loop. Use a labelled for loop here.

no applicable anymore.


query/shortest.go, line 66 at r3 (raw file):

Previously, ashish-goswami (Ashish Goswami) wrote…
func (r *route) indexOf(uid uint64) int {

You can pass a pointer to route that could avoid unnecessary copy.

Done.


query/shortest.go, line 378 at r3 (raw file):

Previously, pawanrawal (Pawan Rawal) wrote…

Is this useful here? We already check this when we append to kroutes above. Does anything fail if you remove this?

Removed.


query/shortest.go, line 389 at r3 (raw file):

// Skip neighbour if the cost is greater than the maximum weight allowed.

query/shortest.go, line 389 at r3 (raw file):

Previously, pawanrawal (Pawan Rawal) wrote…

This needs a comment on what it does and why is it needed. Also mention about the associated test case here.

Added. But didn't mention the test cases. It is being checked in multiple ways.

Copy link
Contributor

@pawanrawal pawanrawal left a comment

Choose a reason for hiding this comment

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

:lgtm_strong:

Reviewed 3 of 3 files at r5.
Reviewable status: all files reviewed, 6 unresolved discussions (waiting on @anurags92, @ashish-goswami, and @gja)


query/query3_test.go, line 1240 at r5 (raw file):

			emptyPath,
		},
		//The test cases below are for k-shortest path queries with varying depths. They don't pass

This comment can be removed

Copy link
Contributor

@manishrjain manishrjain left a comment

Choose a reason for hiding this comment

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

:lgtm: Just ensure that the documentation explains this "changed" behavior.

Reviewable status: 2 of 3 files reviewed, 6 unresolved discussions (waiting on @anurags92, @ashish-goswami, @gja, and @pawanrawal)

Copy link
Contributor

@manishrjain manishrjain left a comment

Choose a reason for hiding this comment

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

Do mention that this is a breaking fix -- if the user's results might change now.

Reviewable status: 2 of 3 files reviewed, 6 unresolved discussions (waiting on @anurags92, @ashish-goswami, @gja, and @pawanrawal)

@all-seeing-code
Copy link
Contributor Author

all-seeing-code commented May 14, 2020

:lgtm: Just ensure that the documentation explains this "changed" behavior.

Reviewable status: 2 of 3 files reviewed, 6 unresolved discussions (waiting on @anurags92, @ashish-goswami, @gja, and @pawanrawal)

Added a documentation JIRA Ticket https://dgraph.atlassian.net/browse/DGRAPH-1369

@all-seeing-code all-seeing-code merged commit 6332254 into master May 15, 2020
all-seeing-code added a commit that referenced this pull request May 29, 2020
* Bug fix for depth parameter
* Handle cycles in K - Shortest path queries

(cherry picked from commit 6332254)
all-seeing-code added a commit that referenced this pull request Jun 8, 2020
* Bug fix for depth parameter
* Handle cycles in K - Shortest path queries

(cherry picked from commit 6332254)
all-seeing-code added a commit that referenced this pull request Jun 9, 2020
…5596)

* K-Shortest path query fix (#5410)

* Bug fix for depth parameter
* Handle cycles in K - Shortest path queries

(cherry picked from commit 6332254)

* Edit test cases to remove flaky behavior (#5464)


(cherry picked from commit bae2b1b)

* Added details of Shortest path queries in documentation (#5533)


(cherry picked from commit abb57c0)
all-seeing-code added a commit that referenced this pull request Jun 9, 2020
…5548)

* K-Shortest path query fix (#5410)

* Bug fix for depth parameter
* Handle cycles in K - Shortest path queries

(cherry picked from commit 6332254)

* Edit test cases to remove flaky behavior (#5464)


(cherry picked from commit bae2b1b)

* Added details of Shortest path queries in documentation (#5533)


(cherry picked from commit abb57c0)
dna2github pushed a commit to dna2fork/dgraph that referenced this pull request Jul 18, 2020
* Bug fix for depth parameter
* Handle cycles in K - Shortest path queries
@all-seeing-code all-seeing-code deleted the anurags92/KshortestPathQuery branch September 24, 2020 11:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants