adjacency-list

How to convert an adjacency matrix to an adjacency list with python?

你说的曾经没有我的故事 提交于 2019-12-10 12:27:14
问题 I have an adjacency matrix like: [[ 0., 15., 0., 7., 10., 0.], [ 15., 0., 9., 11., 0., 9.], [ 0., 9., 0., 0., 12., 7.], [ 7., 11., 0., 0., 8., 14.], [ 10., 0., 12., 8., 0., 8.], [ 0., 9., 7., 14., 8., 0.]] How can I convert it to an adjacency list like this one down here? graph = {'1': [{'2':'15'}, {'4':'7'}, {'5':'10'}], '2': [{'3':'9'}, {'4':'11'}, {'6':'9'}], '3': [{'5':'12'}, {'6':'7'}], '4': [{'5':'8'}, {'6':'14'}], '5': [{'6':'8'}]} ? 回答1: Keep a list of already added edges in a set

What is the most efficient way to determine if a directed graph is singly connected?

a 夏天 提交于 2019-12-09 05:23:12
问题 I am working on an assignment where one of the problems asks to derive an algorithm to check if a directed graph G=(V,E) is singly connected (there is at most one simple path from u to v for all distinct vertices u,v of V. Of course you can brute force check it, which is what I'm doing right now, but I want to know if there's a more efficient way. Could anyone point me in the right direction? 回答1: Have you tried DFS. Run DFS for every vertex in the graph as source If a visited vertex is

Smart pointers for graph representation (vertex neighbors) in C++11

依然范特西╮ 提交于 2019-12-09 01:45:50
问题 I was wondering how to use C++11 smart pointers correctly for graph representations. Suppose, you have a graph structure which contains a vector of all its vertices. Furthermore, you have a structure/class of vertex. This vertex contains a vector of all its neighbors (adjacency list). My question is: which types of pointers/smart pointers should I use, to represent this graph? For binary trees, I read, for parent nodes you should use raw pointers. Because a node doesn't own its parent. The

graphs representation : adjacency list vs matrix

时光怂恿深爱的人放手 提交于 2019-12-08 17:36:30
问题 I'm preparing for a coding interview, and was refreshing my mind on graphs. I was wondering the following : in all places I've seen, it is assumed that adjacency lists are more memory efficient than adjacency matrices for large sparse graphs, and should thus be preferred in that case. In addition, computing the number of outgoing edges from a node requires O(N) in a matrix while it's O(1) in a list, as well as which are the adjacent nodes in O(num adjacent nodes) for the list instead of O(N)

R: Adjacency list to Adjacency matrix

时光怂恿深爱的人放手 提交于 2019-12-08 04:13:49
问题 Bonjour, I would like to convert an adjacency list (3 columns ) to an adjacency matrix. In this forum I have found multiple examples on how to convert an edge list to an adjacency matrix. I successfully managed to do it for a two columns list. I have tried all the solutions I could find on the web but it seems that I missing a little step. What I tried My variables are User, Country, books User<-c("maman","sophia","Antoine") Country<-c("Canada","USA","Mexico") books<-c("Coelho","Rimbaud","The

K-th order neighbors in graph - Python networkx

ε祈祈猫儿з 提交于 2019-12-07 07:58:54
问题 I have a directed graph in which I want to efficiently find a list of all K-th order neighbors of a node. K-th order neighbors are defined as all nodes which can be reached from the node in question in exactly K hops. I looked at networkx and the only function relevant was neighbors . However, this just returns the order 1 neighbors. For higher order, we need to iterate to determine the full set. I believe there should be a more efficient way of accessing K-th order neighbors in networkx . Is

Why are graphs represented using an adjacency list instead of an adjacency set?

廉价感情. 提交于 2019-12-06 08:29:30
If each node maps to a set of the nodes it has edges to, instead of a list, we would gain the ability to gain constant time lookup of edges, instead of having to traverse the whole list. The only disadvantage I can think of is slightly more memory overhead and time to enumerate the edges of a node, but not asymptotically significantly so. I think "list" is just a generic label, not to be taken literally. I've used a Set and it works perfectly well. If I recall correctly, my textbook also used a Set . 来源: https://stackoverflow.com/questions/32588468/why-are-graphs-represented-using-an-adjacency

Count similar adjacent items in List<string>

北慕城南 提交于 2019-12-05 18:21:11
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 j = 0; j < list.Count; j++) { if (list[j] == "b") { if ((list[j + 1] == "b") && (list[j - 1] == "b"))

Deeply nested subqueries for traversing trees in MySQL

狂风中的少年 提交于 2019-12-05 15:58:58
I have a table in my database where I store a tree structure using the hybrid Nested Set (MPTT) model (the one which has lft and rght values) and the Adjacency List model (storing parent_id on each node). my_table (id, parent_id, lft, rght, alias) This question doesn't relate to any of the MPTT aspects of the tree but I thought I'd leave it in in case anyone had a good idea about how to leverage that. I want to convert a path of aliases to a specific node. For example: "users.admins.nickf" would find the node with alias "nickf" which is a child of one with alias "admins" which is a child of

Graph Adjacency List Implementation

时光总嘲笑我的痴心妄想 提交于 2019-12-05 09:53:26
问题 I am trying to represent a Graph in Adjacency List using C#, like the code bellow. But I would like to know where I can find a better implementation in C#. Like this website for Java: http://algs4.cs.princeton.edu/41undirected/Graph.java.html In order to improve this implementation I have some question: Is there another simple data structure to use, and that you able to make operations like DFS , BFS , Find the Shortest-path easier ? Or the data structure vary too much according to the