Questions tagged [graph]

A graph is an abstract representation of objects (vertices) connected by links (edges). For questions about plotting data graphically, use the [data-visualization] tag instead.

Filter by
Sorted by
Tagged with
0
votes
2answers
43 views

Build order from a dependency graph (Python)

Below is my attempt at the challenge found here. The challenge is "Find a build order given a list of projects and dependencies." Already given are the classes Node and Graph (which I can ...
2
votes
1answer
52 views

Turn an array of pairs of start and end points into an ordered series

Given an array of pairs, for example: [["Denver", "Miami"], ["Miami", "Tulsa"], ["LA", "Okmulgee"], ["Mobile", "Portland"...
1
vote
0answers
18 views

Python, create a network with a given node and edges from pandas dataframe

I'm working on a code snippet that generates a network graph. ...
0
votes
1answer
63 views

Cheapest flights within k stops algorithm in JavaScript

The problem: There are n cities connected by some number of flights. You are given an array flights where flights[i] = [fromi, toi, pricei] indicates that there is ...
4
votes
1answer
57 views

Python leetcode: scheduling courses to meet prerequisite requirements

This is my solution to Leetcode #210, Course Schedule II. There are a total of numCourses courses you have to take, labeled from ...
1
vote
1answer
43 views

PureScript breadth-first search implementation

For context, I wrote a function, get_servers, for the game Bitburner which is usually played by writing JavaScript code. Instead, I used PureScript to write the ...
1
vote
1answer
50 views

Checking whether a graph is connected in an immutable way

I have this function at the moment ...
0
votes
0answers
64 views

Graph Implementation for Social Network

I'm trying to implement a graph structure in python, making use of the classes Vertex, Edge and Graph. The goal is to read a csv file that contains users names and a number (weight), like such Lynch,...
8
votes
1answer
168 views

Find all paths in a graph

My code does a DFS on the given graph and prints all possible paths for a given start and end node. My question is how can this be improved? Can some form of memoization be used here? There are cases ...
2
votes
0answers
30 views

Dijkstra from scratch

I implemented the Dijkstra algorithm from scrath and wonder if I can save some code? The whole algorithm is here. n is the number of nodes, and m the number of edges. 1 ≤ 𝑛 ≤ 10^4, 0 ≤ 𝑚 ≤ 10^5, 𝑢 ...
0
votes
0answers
34 views

Enumerating all fixed-length paths from a vertex in a graph

Enumerating all fixed-length paths from a vertex in a graph 1. Graph-related classes Base implementations of the relevant classes in my graph library. I'm using C#9 in a .NET 5 project. I opted to use ...
4
votes
2answers
115 views

Leetcode: number of islands

I am on my second iteration of the famous "Number Of Islands" graph problem on Leetcode. https://leetcode.com/problems/number-of-islands/ ...
2
votes
0answers
73 views

Slow render of a graph path using python matplotlib animation

I am computing shortest paths of graphs using Python Networkx and Matplotlib. I'm focused on animating the route inspection problem path in particular here. My goal is to smoothly render such ...
0
votes
0answers
14 views

Add an edge to a directed acyclic graph such that it stay acyclic

My code doesn't actually do that per se, but rather it find every possible edge that can be added without creating a cycle. The return value of the function ...
3
votes
1answer
101 views

Optimizing Dijkstra on grid Python (Microsoft Interview)

Given a square grid of size N, each cell of which contains integer cost which represents a cost to traverse through that cell, we need to find a path from top left cell to bottom right cell by which ...
3
votes
1answer
53 views

Collect distances from 1 point to all others in a graph

Problem: I have a weighted graph. I want to get distance from definite point to all other points in the graph.(and then get path to them) I used modified dijkstra algorithm So, here is code: ...
0
votes
1answer
65 views

BFS implementation in JAVA

...
-3
votes
1answer
217 views

HackerRank | Roads and Libraries - Code Optimization

Major Edit: I wish people threw in a comment regarding what they are expecting instead of downvoting this question. I am quite appreciative of constructive feedback. I know it is only 3 people out of ...
0
votes
2answers
55 views

Generating a random directed graph

I wrote a function that returns a directed graph as an adjacency matrix. The function requires two arguments: the amount of nodes and the amount of edges. At first, nodes are placed and instantly ...
2
votes
1answer
107 views

Dijkstra's algorithm in graph (Python)

I need some help with the graph and Dijkstra's algorithm in Python 3. I tested this code (look below) at one site and it says to me that the code took too long. Can anybody suggest how to solve that? ...
3
votes
0answers
75 views

Modified Tarjan's algorithm for reporting the edges within the strongly connected component

As described in his paper, Tarjan's algorithm reports only the nodes of the strongly connected components. For my use, I would also like to also have the edge list of the components (so that I can run ...
5
votes
1answer
151 views

Kings Tour Python

This is a depth-first search Kings Tour. I call it like this because the king must pass by all coordinates only touching them once (like a knights tour). How could I improve? Thank You ...
1
vote
0answers
40 views

Dijkstra's shortest-path algorithm implemented via Priority Queues and an adjacency matrix represented graph

My adjacency matrix looks like this, where non-zero weights denote edges. [0, 2, 0, 0, 3] [2, 0, 2, 4, 0] [0, 2, 0, 3, 0] [0, 4, 3, 0, 1] [3, 0, 0, 1, 0] Here's my attempt, would appreciate a code ...
2
votes
1answer
462 views

Prim's algorithm via Priority Queues to print the minimum spanning tree of an adjacency matrix undirected graph

My adjacency matrix looks like this, where non-zero weights denote edges. [0, 2, 0, 0, 3] [2, 0, 2, 4, 0] [0, 2, 0, 3, 0] [0, 4, 3, 0, 1] [3, 0, 0, 1, 0] Here's my attempt: ...
3
votes
2answers
108 views

Prim's algorithm via Priority Queues to print the minimum spanning tree of an undirected graph

Here's my attempt, I believe the runtime is O(V*(ElogE + log E)) since the while loop runs V times and it takes ElogE + log E to do the priority queue operations inside the loop. ...
6
votes
1answer
120 views

Graph implementation with Dijkstra

So, here is the code: ...
3
votes
1answer
55 views

Detecting a cycle in a directed graph via an adjacency list implementation and a recursive DFS

Here's my attempt. I iterate through each vertex in the graph and do a DFS to see if I reach back on a vertex I already visited in this vertex's iteration. It seems to work but I am not satisfied with ...
7
votes
1answer
201 views

Path finder for network link wiring

I have created a Path finder. The aim is to find ALL paths that join a Point A to a Point B. I have defined an object Segment which is a link between two assets. It ...
2
votes
0answers
25 views

Data Visualization (rendering env)

How can I improve this code? It renders a ML env with openAI gym and matplotlib. I am new to coding so not sure if my variables or format or any lines can be improved. ...
7
votes
1answer
93 views

Cycle detection in undirected graphs with recursive DFS

I wrote a very simple implementation of cycle detection in an undirected graph; I'm not interested in applying it to any real-life case: it's just for explaining the basic idea behind cycle detection ...
3
votes
0answers
45 views

LeetCode: Number of enclaves C#

https://leetcode.com/problems/number-of-enclaves/ Given a 2D array A, each cell is 0 (representing sea) or 1 (representing land) A move consists of walking from one land square 4-directionally to ...
6
votes
1answer
93 views

Graph: Depth First Search (N citizens with pairs of friends)

I have solved problem 10608 on UVA Online Judge using Python 3.5.1. My solution works, but it takes too long to run when the online judge evaluates it. Problem There is a town with N citizens. It is ...
4
votes
1answer
176 views

Graph implementation in C

I've recently been learning C on my own, and I thought I'd try my hand at writing a graph implementation. ...
4
votes
1answer
103 views

A graphing calculator

I wrote a calculator that can solve equations with variables using matplotlib and sympy. I am looking for tips on performance and style. How can I improve on this calculator? Is it a good idea to use ...
6
votes
1answer
334 views

Air Quality/Environment Monitoring Web App using Plotly Dash

Overview & Motivation This project manages and displays sensor data (temperature, humidity, Air Quality Index). The sensor data is displayed by the sensor manufacturer, PurpleAir, but their tool ...
3
votes
1answer
326 views

Implementing a Directed and Undirected Graph in Java

I am learning Graphs on my own and want to use a graph for a personal small project, so I decided to take a look here in Code Review for some cool implementations and came across this one by Maksim ...
1
vote
0answers
53 views

Finding strongly connected components in a directed graph using Kosaraju's algorithm

I'm working through Stanford's algorithms course on edX. One of the programming assignments is to find strongly connected components using Kosaraju's algorithm, on a graph with ~875,000 vertices. My ...
1
vote
0answers
13 views

LP constraint problem with scipy

For this graph and its vertex cover I try to compare the LP solution with the optimal and the rounded solution. ...
4
votes
2answers
1k views

Floyd-Warshall Path Reconstruction

Below is the implementation for the Floyd-Warshall algorithm, which finds all-pairs shortest paths for a given weighted graph. The function floyd_warshall takes a ...
4
votes
1answer
117 views

Extreme use of pointers in adjacency list graph construction?

Playing around with pointers in C++, implemented an adjacency list graph. Am I going too far/extreme trying to place data onto the heap? graph.h ...
3
votes
2answers
398 views

Independent cascade model for networkx graphs

Similar to this question, I implemented the independent cascade model, but for a given networkx graph (including multigraph with parallel edges). My focus is on readability, pythonic-ness, and ...
5
votes
1answer
69 views

Closest Pair heuristics, graph with adjacency matrix in C++17

I was trying to solve a problem that was briefly mentioned at the beginning of the "The Algorithm Design Manual" by Steven Skiena (Ch 1, Problem 26). It took me some time to build a working ...
1
vote
0answers
36 views

Representing a graph with passable marks and actions

I wanted to have a framework to build Petri Nets from C# code. Petri net is a digraph with functional transitions and mark types. In books they tend to look like this: Here is what I came up with - ...
4
votes
0answers
86 views

Converting a graph to a directed graph with order of the nodes defined by post-order traversal

Background: I'm implementing an algorithm from the paper Polygon Area Decomposition for Multiple-Robot Workspace Division and one of the steps is to represent a polygon as a graph. This is done by ...
8
votes
2answers
382 views

My Graph Implementation In Python

I have implemented a graph based on theory, and I'd like some advice regarding best practice, and thoughts about what i didn't implement that would make it even more useful, along with ideas that are ...
11
votes
1answer
211 views

Create a build order from a list of projects and their respective prerequisites

Another graph algorithm, this time to create a priority or build order. Provided with a starting List<Project> and ...
7
votes
1answer
405 views

Dijkstra algorithm C#

I have implementing a simple version of Dijkstra's algorithm in C#. Could this be made more efficient? Does it need to be modified? ...
2
votes
1answer
146 views

Trie implementation for strings in Swift

A trie for handling strings for an autocomplete dictionary. This passes my fairly casual tests, though it's always possible that there are broken edge cases, but I'm mainly concerned about design and ...
3
votes
0answers
116 views

Breadth First Search Python Implementation

I have the below implementation of the BFS algorithm, in which I've used OrderedDict instead of a list or ...
3
votes
1answer
157 views

Test the hypothesis that the expected number of edges of a random connected graph is \$ O(n \log n) \$

Motivation The most common model for a random graph is the Erdős–Rényi model. However, it does not guarantee the connectedness of the graph. Instead, let's consider the following algorithm (in python-...

1
2 3 4 5
14