Skip to content

feat: adding RootingTreeFromGraph algorithm. #1665

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

Closed
wants to merge 1 commit into from
Closed
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
59 changes: 59 additions & 0 deletions Trees/RootingTreeFromGraph.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Author: Denton Kunz
* Tree rooting algorithm in Javascript
* Takes in an undirected graph (adjacency matrix)
* Follows pseudocode given at https://youtu.be/2FFq2_je7Lg?si=rIwT8UCYcaGhxH6h
* Node class can be modified to include a data field
*/

class Node {
constructor(id, parent, children=[]){
this.id = id
this.parent = parent
this.children = children
}
}

function rootTree(graph, rootId=0){

console.log(graph)

//the first node (root) should start with no parents/children
const root = new Node(rootId, null)
//recursively generate the rest of the tree
return buildTree(graph, root, null)
}

function buildTree(graph, node, parent){
//let i represent the id of the child node
for (let i = 0; i < graph[node.id].length; i++) {
//skip until we find a child node
if(graph[node.id][i]==0){
// console.log("skipped 1")
continue
}

//if the child node is the current node, skip this case
if(node.id == i){
// console.log("skipped 2")
continue
}

//avoid creating an edge between child and the current node's parent
if( parent != null && i == parent.id){
// console.log("skipped 3")
continue
}

//create and add the new node
let child = new Node(i, node)
node.children.push(child)

//recursively iterate using that node
buildTree(graph, child, node)
}

return node
}

export {Node, rootTree, buildTree}
29 changes: 29 additions & 0 deletions Trees/test/RootingTreeFromGraph.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {rootTree} from '../RootingTreeFromGraph'

describe('Test 1', () =>{
/* Graph structure:
*
* 0 3 - 4
* \ /
* 1
* /
* 2
*/
const myGraph = [[1,1,0,0,0],
[1,1,1,1,0],
[0,1,1,0,0],
[0,1,0,1,1],
[0,0,0,1,1]]

it('testing', () => {
const res = rootTree(myGraph)
expect(res.id).toStrictEqual(0)
expect(res.children.length).toStrictEqual(1)
expect(res.children[0].id).toStrictEqual(1)
expect(res.children[0].children.length).toStrictEqual(2)
expect(res.children[0].children[0].id).toStrictEqual(2)
expect(res.children[0].children[1].id).toStrictEqual(3)
expect(res.children[0].children[1].children.length).toStrictEqual(1)
expect(res.children[0].children[1].children[0].id).toStrictEqual(4)
})
})
Loading