adjacency-list

Count similar adjacent items in List<string>

淺唱寂寞╮ 提交于 2019-12-22 08:38:46
问题 I'm trying to find similar adjacent items in List and count its number, e.g.: List<string> list = new List<string> {"a", "a", "b", "d", "c", "c"}; Desired Output: a = 2, c = 2 What I've done is use for loop to iterate over each element of the list and to see whether it has similar adjacent element, but understandably it gives ArgumentOutOfRangeException() because I don't know how to keep track of the position of the iterator so that it doesn't go out of bounds. Here's what I've done: for (int

Find all nodes in an adjacency list model with oracle connect by

喜夏-厌秋 提交于 2019-12-21 06:38:07
问题 Given the following model: create table child_parent ( child number(3), parent number(3) ); Given the following data: insert into child_parent values(2,1); insert into child_parent values(3,1); insert into child_parent values(4,2); insert into child_parent values(5,2); insert into child_parent values(6,3); results in the following tree: 1 / \ 2 3 / \ \ 4 5 6 Now i can find the parents of 5 like this: SELECT parent FROM child_parent START WITH child = 5 CONNECT BY NOCYCLE PRIOR parent = child;

Adjacency List to JSON graph with Postgres

一个人想着一个人 提交于 2019-12-20 12:33:08
问题 I have the following schema for the tags table: CREATE TABLE tags ( id integer NOT NULL, name character varying(255) NOT NULL, parent_id integer ); I need to build a query to return the following structure (here represented as yaml for readability): - name: Ciencia parent_id: id: 7 children: - name: Química parent_id: 7 id: 9 children: [] - name: Biología parent_id: 7 id: 8 children: - name: Botánica parent_id: 8 id: 19 children: [] - name: Etología parent_id: 8 id: 18 children: [] After some

Recursive PHP function for adjacency-list display

空扰寡人 提交于 2019-12-20 06:45:45
问题 I have a DB like so: id text parent 1 Parent 1 0 2 Child of 1 1 3 Sibling 1 4 Another Parent 0 5 A first child 4 So I'm trying to capture a tree structure my listing the parents. I'm aware of the other option (nested sets I think?) but I'm going to stick with this for now. I'm now trying to get the data out of the DB and into a nested array structure in PHP. I have a function like this: class Data_Manager { public $connection = ''; public $collection = array(); function __construct() { $this-

Graph representation benchmarking

馋奶兔 提交于 2019-12-19 06:45:07
问题 Currently am developing a program that solves (if possible) any given labyrinth of dimensions from 3X4 to 26x30. I represent the graph using both adj matrix (sparse) and adj list. I would like to know how to output the total time taken by the DFS to find the solution using one and then the other method. Programatically, how could i produce such benchmark? 回答1: An useful table to work out with various graphs implementations: OPERATION EDGE LIST ADJ LIST ADJ MATRIX degree(v) O(m) O(d(v)) O(n)

Most efficient way of creating tree from adjacency list

丶灬走出姿态 提交于 2019-12-18 11:42:56
问题 I have an adjacency list of objects (rows loaded from SQL database with the key and it's parent key) that I need to use to build an unordered tree. It's guaranteed to not have cycles. This is taking wayyy too long (processed only ~3K out of 870K nodes in about 5 minutes). Running on my workstation Core 2 Duo with plenty of RAM. Any ideas on how to make this faster? public class StampHierarchy { private StampNode _root; private SortedList<int, StampNode> _keyNodeIndex; // takes a list of nodes

Detecting cycles in a graph using DFS: 2 different approaches and what's the difference

。_饼干妹妹 提交于 2019-12-18 09:55:25
问题 Note that a graph is represented as an adjacency list. I've heard of 2 approaches to find a cycle in a graph: Keep an array of boolean values to keep track of whether you visited a node before. If you run out of new nodes to go to (without hitting a node you have already been), then just backtrack and try a different branch. The one from Cormen's CLRS or Skiena: For depth-first search in undirected graphs, there are two types of edges, tree and back. The graph has a cycle if and only if there

Detecting cycles in a graph using DFS: 2 different approaches and what's the difference

浪尽此生 提交于 2019-12-18 09:55:09
问题 Note that a graph is represented as an adjacency list. I've heard of 2 approaches to find a cycle in a graph: Keep an array of boolean values to keep track of whether you visited a node before. If you run out of new nodes to go to (without hitting a node you have already been), then just backtrack and try a different branch. The one from Cormen's CLRS or Skiena: For depth-first search in undirected graphs, there are two types of edges, tree and back. The graph has a cycle if and only if there

How do you convert a parent-child (adjacency) table to a nested set using PHP and MySQL?

放肆的年华 提交于 2019-12-17 17:33:45
问题 I've spent the last few hours trying to find the solution to this question online. I've found plenty of examples on how to convert from nested set to adjacency... but few that go the other way around. The examples I have found either don't work or use MySQL procedures. Unfortunately, I can't use procedures for this project. I need a pure PHP solution. I have a table that uses the adjacency model below: id parent_id category 1 0 Books 2 0 CD's 3 0 Magazines 4 1 Books/Hardcover 5 1 Books/Large

Adjacency List and Adjacency Matrix in Python

独自空忆成欢 提交于 2019-12-17 10:42:24
问题 Hello I understand the concepts of adjacency list and matrix but I am confused as to how to implement them in Python: An algorithm to achieve the following two examples achieve but without knowing the input from the start as they hard code it in their examples: For adjacency list: a, b, c, d, e, f, g, h = range(8) N = [ {b:2, c:1, d:3, e:9, f:4}, # a {c:4, e:3}, # b {d:8}, # c {e:7}, # d {f:5}, # e {c:2, g:2, h:2}, # f {f:1, h:6}, # g {f:9, g:8} # h ] For adjacency matrix: a, b, c, d, e, f, g