graph-theory

Storing only unique/non-isomorphic undirected networkx graphs?

Storing only unique/non-isomorphic undirected networkx graphs? Question: Context After thinking about the most efficient way to store unique graphs (in plain text), I determined the following procedure: create a folder with the graph size (nr of nodes) as name when storing a new graph of size n, load the graphs inside the folder of graph …

Total answers: 1

What is the benefit of using complex numbers to store graph coordinates?

What is the benefit of using complex numbers to store graph coordinates? Question: I am looking at a solution to an Advent of Code puzzle that stores coordinates as complex numbers: heightmap = { complex(x, y): c for y, ln in enumerate(sys.stdin.read().strip().split("n")) for x, c in enumerate(ln) } Then accesses them later as follows: for …

Total answers: 1

Create an edge object without an implicit bias towards a side in Python

Create an edge object without an implicit bias towards a side in Python Question: Python noob here. I am trying to create a network object in Python that contains both node and edge objects. Edges are constructed from two nodes. Networks are constructed from lists of nodes and edges. My problem arises where the network …

Total answers: 1

Python. DFS graph traversal, correct output?

Python. DFS graph traversal, correct output? Question: I’m currently getting to grips with graph traversal in Python. Given the following graph: Implemented using this dictionary: graph = {‘0’: set([‘1’, ‘2’, ‘3’]), ‘1’: set([‘0′,’2’]), ‘2’: set([‘0′,’1′,’4’]), ‘3’: set([‘0’]), ‘4’: set([‘2’])} Am I correct in thinking a depth first search traversal beginning from node 0 should return …

Total answers: 2

Prevent single name being left over in Secret Santa algorithm?

Prevent single name being left over in Secret Santa algorithm? Question: This year, I decided to write up a program to automate our family’s secret santa name drawings by sending each person an email of who they got without anyone else knowing. At first I thought this would be trivially easy, but it has led …

Total answers: 3

Python code to do breadth-first discovery of a non-binary tree

Python code to do breadth-first discovery of a non-binary tree Question: My problem: I have a known root node that I’m starting with and a specific other target node that I’m trying to find the shortest path to. I’m trying to write Python code to implement the Iterative Deepening Breadth-First Search algo, up to some …

Total answers: 2

How to represent graph's node/vertex as alphabetic or name value instead of numeric

How to represent graph's node/vertex as alphabetic or name value instead of numeric Question: This is the code I wrote for Graph structure, which works in the value passed in numeric class Graph: def __init__(self, nVertices): self.nVertices = nVertices self.adjList = [[]for i in range(nVertices)] def addEdge(self, v1, v2): self.adjList[v1].append(v2) self.adjList[v2].append(v1) def dfs(self, sv, visited): …

Total answers: 1

Adjacency matrix with string nodes in Pythom

Adjacency matrix with string nodes in Python Question: I have a csv file of nodes and a csv file of edges – all the nodes are STRING. I need help please for how can I build an adjacency matrix of this graph in python? Example of the data: Nodes csv: a b c d Edges …

Total answers: 1

Recursive DFS into Iterative DFS with global state

Recursive DFS into Iterative DFS with global state Question: Here is this graph algorithm that finds a path between two nodes in a DAG. from typing import * def find_path(graph: List[List[int]], start: int, end: int) -> List[int]: path = [] def dfs(node): path.append(node) if node == end: return True for neighbor in graph[node]: if dfs(neighbor): …

Total answers: 2