## Why does the formula floor((i-1)/2) find the parent node in a binary heap?

I learned that when you have a binary heap represented as a vector / list / array with indicies [0, 1, 2, 3, 4, 5, 6, 7, 8, …] the index of the parent of element at index i can be found with parent index = floor((i-1)/2)

I have tried to explain why it works. Can anyone help me verify this?

Took reference from Why does the formula 2n + 1 find the child node in a binary heap? thanks to @Giulio

## Question about numbering of internal nodes in circuit diagrams when one circuit element has more than 1 internal node

I have the following circuit diagram that I’ve added labels for the internal nodes to using outside sources. I understand what an internal node is, however what I’m confused about is– for example– E1 has 2 wires connected to another circuit element, so they’re both internal nodes, but how do I distinguish which is n1? Or is this completely arbitrary?

And why is n2 after E4 here, is it because it has to follow the flow of the circuit?

Sorry for the noob question, my book doesn’t explain this (at least so I’ve read, I could be missing it).

## Minimum number of nodes to select such that every node is at most k nodes away

I received this problem on an exam a few months ago, and have kept thinking about how to solve it with no luck.

Given a binary tree where each node in the tree can either be selected or unselected, implement a function k_away() which returns the minimum number of nodes that need to be selected so that every node in the tree is at most k nodes away from a selected node.

So, the nodes simply contain a pointer to the left child, a pointer to the right child, and a boolean marking it as selected or not:

struct Node {     Node *left;     Node *right;     bool selected = false; // starts out false }; 

The problem specifies a constraint of having a time complexity of O(n) and an auxiliary space complexity of O(n).

What I’ve thought of so far:

• It seems like there are 2^n potential solutions (if I can choose to either select or not select every node and there are 2 of them), so brute force is a bad idea
• I’ve searched around for similar problems and the closest thing I can find is the Dominating Set problem which seems… impossible to solve at this moment in polynomial time. I doubt this problem was given as an impossible problem.
• Running DFS to get to the leaf nodes, then counting height as recursion unrolls. Every k away, marking the node as selected. This seems to work on small test cases, but does not return the minimum number away.
• Running BFS on the root node to find all nodes k away while using another data structure to mark all visited nodes as ‘covered’, and then recursively running the BFS on each k-away node. This also seems to work on small test cases, but again doesn’t return the minimum number away.

## shortest path algorithm – why backtrack from the end node instead of starting from the starting node?

I was watching a dynamic programming video by Erik Demaine . He says here https://youtu.be/OQ5jsbhAv_M?t=2133 , finding the shortest path by guessing the node after the starting node is not the right approach, and instead should guess the node before the last node. I didn’t understand his explanation. Can someone explain better why find the path backwards? It seems to me that you should get the same answer either way and both approaches are equally good.

## algorithm to find shortest path connecting EVERY node

I have received a problem to solve and I am not sure what algorithm to use.

TLDR; Find the shortest path to get to every node in a undirected graph

The problem states that one must visit every station in the shanghai metro in the shortest path possible. Interchange Stations (‘edges’) can be reused and you can start / stop anywhere.

I have created a lookup table that shows connected stations as well as the distance to travel (not shown)

"Xinzhuang": [   "Waihuan Rd." : 1 ], "Waihuan Rd.": [   "Xinzhuang": 2.2,   "Lianhua Rd.": 3 ], "Lianhua Rd.": [   "Waihuan Rd.": 4,   "Jinjiang Park": 5, ], "Jinjiang Park": [   "Lianhua Rd.": 9.1,   "South Railway Station": 10.3 ], "South Railway Station": [   "Jinjiang Park": 4.1,   "Caobao Rd.": 1.1,   "Shilong Rd.": 2.5 ], ... 

I found this leetcode problem but it did not mention any specific algorithm and since it was O(2^N * N) I wondered if there was a faster method than BFS.

https://leetcode.com/problems/shortest-path-visiting-all-nodes/solution/

Since my graph is so big, I was going to reduce the lines with a single path to a single node.

What algorithm can I use that will work in Polynomial time, OR has the least time complexity?

## Number of graphs that satisfies the property that edge weight is maximum of node values on which the edge is incident

I have an undirected weighted graph without multi edges. All the edge weights are whole numbers and known. I want to know in how many ways node values(node values are also whole numbers) can be assigned to the nodes such that the graph satisfies the condition that for every edge its edge weight is exactly equal to maximum of two node values this edge is incident on.

## Building Suffix Array from Suffix Tree. Inorder visit when node has more than two children

From the notes:

It is not difficult to observe that the suffix array $$\texttt{SA}$$ of the text $$\texttt{T}$$ can be obtained from its suffix tree $$\texttt{ST}$$ by performing an in-order visit: each time a leaf is encountered, the suffix-index stored in this leaf is written into the suffix array $$\texttt{SA}$$; each time an internal node u is encountered, its associated value is written into the array $$\texttt{lcp}$$.

How do you do the inorder visit when a node has more than two children?

Let’s say you visit the leftmost child, then the node, then the other leftmost child. Then do you visit again the node?

SA in an array of pointer to suffixes, ordered lexicographically.

lcp contains the longest common prefix between two consecutive suffixes $$\text{suff}_{SA[i]} \text{ and } \text{suff}_{SA[i+1]}$$.

The $represents a char that’s smaller than every other char. The value associated to each node is the length of the prefix spelled so far. The leaves represents indices of suffixes. If T = banana$ , the leaf 3 represents nana$(T[3,7]) In the image there’s what is supposed to be a suffix tree, but I think there’s an error since the edges should be sorted according to their label and the leaf 7 with the edge labeled "$ " should be the leftmost leaf and edge.

When simulating the algorithm, first you visit the node 7 ( using the fixed version of the tree ), then the root, so you have

SA = [7, ...] lcp = [0, ...] 

Then let’s say you keep going on with an inorder visit of u. When you go back to the root, do you insert the value 0 again in the lcp? Or do you do it just the first time you visited the root?

In other words, do I visit child-root-child-root… or child-root-child-child…

## What is the upper bound on the number of nodes in a tree with n leaves where each internal node has at least two children?

The pieces of information available to us are the number of leaves in a tree and that each internal node must have at least two children. Is there a way to find the upper bound on the total number of nodes in the tree?

## how to send prime and generator of diffie hell-men to client over network node js?

I am using crypto module of node js for exchanging key using diffie-hellman algorithm.

server.js

const crypto = require("crypto");  const alice = crypto.createDiffieHellman(512);  const aliceKey = alice.generateKeys(); 

client.js

const bob = crypto.createDiffieHellman(alice.getPrime(), alice.getGenerator());  const bobKey = bob.generateKeys();  const aliceSecret = alice.computeSecret(bobKey);  const bobSecret = bob.computeSecret(aliceKey); 

The above example is taken from node.js documentation as shown the client uses servers prime number for generating the prime number.

my question is how should I securely send the prime number and the other parameter to client over internet? are there any other alternatives?

and another question is that I am generating keys using generate keys function but I have already generated private-key.pem and public-cert.pem file. can I use those if yes then how?, if no then what is difference between those keys?

## How to install node modules for external evaluation?

I was hoping for some extra clarity configuring additional modules for use in external evaluation, since there’s no mention of this in the the nodejs workflow.

It seems mathematica looks only at ~/.node_modules, so is it sufficient to simply generate a soft link?

cd ~; ln -s node_modules .node_modules 

In decreasing importance, here are some specific sub-questions:

1. I prefer using yarn to manage required installations, do I have to use npm?
2. Do npm installs need to be global or user?
3. Does the fe or kernel need to be restarted; or evaluators re-registered after install or removal?

Knowing that any specific functionalities or modules will definitely not work (e.g. no browser …) would help, and any pro-tips from Javascript developers would be appreciated. For example, would it be possible to add a 'package.json' file for a notebook and isolate dependencies? I really don’t want these mma configuration steps to interfere with my existing node-js work.