From 585bdcc3fb8e7f31988421f2f398699c3db923c9 Mon Sep 17 00:00:00 2001 From: Denton Date: Mon, 20 May 2024 19:27:12 -0700 Subject: [PATCH] feat: adding RootingTreeFromGraph algorithm. --- Trees/RootingTreeFromGraph.js | 59 +++++++++++++++++++++++++ Trees/test/RootingTreeFromGraph.test.js | 29 ++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 Trees/RootingTreeFromGraph.js create mode 100644 Trees/test/RootingTreeFromGraph.test.js diff --git a/Trees/RootingTreeFromGraph.js b/Trees/RootingTreeFromGraph.js new file mode 100644 index 0000000000..a6a09bc78b --- /dev/null +++ b/Trees/RootingTreeFromGraph.js @@ -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} \ No newline at end of file diff --git a/Trees/test/RootingTreeFromGraph.test.js b/Trees/test/RootingTreeFromGraph.test.js new file mode 100644 index 0000000000..25e80894e1 --- /dev/null +++ b/Trees/test/RootingTreeFromGraph.test.js @@ -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) + }) +}) \ No newline at end of file