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

add method to get vertices sorted by their degree #68

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/kotlin/sipura/alg/Traversal.kt
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,8 @@ object Traversal {
}
}
}

fun <V> degreeSort(g: SimpleGraph<V>, ascending: Boolean = true): List<V> {
return if (ascending) g.V.sortedBy { g.degreeOf(it) } else g.V.sortedByDescending { g.degreeOf(it) }
}
}
18 changes: 18 additions & 0 deletions src/test/kotlin/sipura/alg/TraversalTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import sipura.Samples
import sipura.alg.Traversal.degreeSort
import sipura.generate.Factory
import kotlin.test.assertContains
import kotlin.test.assertEquals
Expand Down Expand Up @@ -144,4 +145,21 @@ internal class TraversalTest {
assertFalse { iter.hasNext() }
}
}

@Nested
internal inner class VerticesOrderedByDegree {

@Test
fun `vertices sorted by degree`() {
assertEquals(degreeSort(Samples.star4Plus1IsolatedVertex()).last(), 1)
assertEquals(degreeSort(Samples.star4Plus1IsolatedVertex(), ascending = false).first(), 1)
}

@Test
fun `path of length 3`() {
val path3 = Factory.createPathGraph(3)
assertEquals(degreeSort(path3).last(), 2)
assertEquals(degreeSort(path3, ascending = false).first(), 2)
}
}
}