adjacency-list

Create graph using adjacency list

和自甴很熟 提交于 2019-11-30 19:36:02
问题 #include<iostream> using namespace std; class TCSGraph{ public: void addVertex(int vertex); void display(); TCSGraph(){ head = NULL; } ~TCSGraph(); private: struct ListNode { string name; struct ListNode *next; }; ListNode *head; } void TCSGraph::addVertex(int vertex){ ListNode *newNode; ListNode *nodePtr; string vName; for(int i = 0; i < vertex ; i++ ){ cout << "what is the name of the vertex"<< endl; cin >> vName; newNode = new ListNode; newNode->name = vName; if (!head) head = newNode;

Get contagion chain from adjacency matrix, r, igraph

我的梦境 提交于 2019-11-30 07:42:30
Q.I have a erdos.reyni graph. I infect a vertex and want to see what sequence of vertices the disease would follow? igraph has helful functions like get.adjacency(), neighbors(). Details. This is the adjacency matrix with vertex names instead of 0,1 flags and i'm trying to get the contagion chain out of it. Like the flow/sequence of an epidemic through a graph if a certain vertex is infected. Let's not worry about infection probabilities here (assume all vertices hit are infected with probability 1). So suppose I hit vertex 1 (which is row 1 here). We see that it has outgoing links to vertex 4

Making an adjacency list in C++ for a directed graph

北城余情 提交于 2019-11-30 02:39:08
问题 Hello all :) Today I am refining my skills on graph theory and data structures. I decided to do a small project in C++ because it's been a while since I've worked in C++. I want to make an adjacency list for a directed graph. In other words, something which looks like: 0-->1-->3 1-->2 2-->4 3--> 4--> This would be a directed graph with V0 (vertex 0) having an edge to V1 and V3, V1 having an edge to V2, and V2 having an edge to V4, like this: V0----->V1---->V2---->V4 | | v V3 I know that in

Managing hierarchies in SQL: MPTT/nested sets vs adjacency lists vs storing paths

旧城冷巷雨未停 提交于 2019-11-30 01:25:21
For a while now I've been wrestling with how best to handle hierarchies in SQL. Frustrated by the limitations of adjacency lists and the complexity of MPTT/nested sets, I began thinking about simply storing key paths instead, as a simple node_key/node_key/... string. I decided to compile the pros and cons of the three techniques: Number of calls required to create/delete/move a node: Adjacency = 1 MPTT = 3 Path = 1 (Replace old node path with new node path across all nodes that contain that path) Number of calls required to get a tree: Adjacency = [number of sub-levels] MPTT = 1 Path = 1

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

隐身守侯 提交于 2019-11-29 18:52:53
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 exists a back edge. Can somebody explain what are the back edges of a graph and what's the diffirence

BFS traversal of all paths in graph using adjacency list

坚强是说给别人听的谎言 提交于 2019-11-29 12:10:43
I am currently trying to traverse all paths from source to destination in a graph which uses adjacency matrix. I have been trying to do it in BFS way.Thanks for the help. I am getting only one path. How do I get to print other paths as well ? public class AllPossiblePaths { static int v; static ArrayList<Integer> adj[]; public AllPossiblePaths(int v) { this.v = v; adj = new ArrayList[v]; for (int i = 0; i < v; i++) { adj[i] = new ArrayList<>(); } } // add edge from u to v public static void addEdge(int u, int v) { adj[u].add(v); } public static void findpaths(int source, int destination) {

Get contagion chain from adjacency matrix, r, igraph

柔情痞子 提交于 2019-11-29 10:25:14
问题 Q.I have a erdos.reyni graph. I infect a vertex and want to see what sequence of vertices the disease would follow? igraph has helful functions like get.adjacency(), neighbors(). Details. This is the adjacency matrix with vertex names instead of 0,1 flags and i'm trying to get the contagion chain out of it. Like the flow/sequence of an epidemic through a graph if a certain vertex is infected. Let's not worry about infection probabilities here (assume all vertices hit are infected with

Managing hierarchies in SQL: MPTT/nested sets vs adjacency lists vs storing paths

天大地大妈咪最大 提交于 2019-11-28 21:40:47
问题 For a while now I've been wrestling with how best to handle hierarchies in SQL. Frustrated by the limitations of adjacency lists and the complexity of MPTT/nested sets, I began thinking about simply storing key paths instead, as a simple node_key/node_key/... string. I decided to compile the pros and cons of the three techniques: Number of calls required to create/delete/move a node: Adjacency = 1 MPTT = 3 Path = 1 (Replace old node path with new node path across all nodes that contain that

How to create weighted adjacency list/matrix from edge list?

倖福魔咒の 提交于 2019-11-28 17:07:05
My problem is very simple: I need to create an adjacency list/matrix from a list of edges. I have an edge list stored in a csv document with column1 = node1 and column2 = node2 and I would like to convert this to a weighted adjacency list or a weighted adjacency matrix. To be more precise, here's how the data looks like -where the numbers are simply node ids: node1,node2 551,548 510,512 548,553 505,504 510,512 552,543 512,510 512,510 551,548 548,543 543,547 543,548 548,543 548,542 Any tips on how to achieve the conversion from this to a weighted adjacency list/matrix? This is how I resolved to

Flatten Adjacency List Hierarchy To A List Of All Paths

*爱你&永不变心* 提交于 2019-11-28 16:55:23
I have a Table that stores Hierarchical information using the Adjacency List model. (uses a self referential key - example below. This Table may look familiar ): category_id name parent ----------- -------------------- ----------- 1 ELECTRONICS NULL 2 TELEVISIONS 1 3 TUBE 2 4 LCD 2 5 PLASMA 2 6 PORTABLE ELECTRONICS 1 7 MP3 PLAYERS 6 8 FLASH 7 9 CD PLAYERS 6 10 2 WAY RADIOS 6 What is the best method to "flatten" the above data into something like this? category_id lvl1 lvl2 lvl3 lvl4 ----------- ----------- ----------- ----------- ----------- 1 1 NULL NULL NULL 2 1 2 NULL NULL 6 1 6 NULL NULL 3