# 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.

606 questions
Filter by
Sorted by
Tagged with
979 views

### Python breadth first search algorithm

Any pointers, or any advice on how to clean up my code/make more pythonic would be appreciated. ...
20k views

### Adjacency List Graph representation on python

I began to have my Graph Theory classes on university, and when it comes to representation, the adjacency matrix and adjacency list are the ones that we need to use for our homework and such. At the ...
385 views

### Forming sets from pairs of numbers

I do not want to use a new data structure but the existing APIs in Java. Here's a solution. I am thinking if this can be made simpler. Input say ...
1k views

### DFS and BFS in a graph in C++

Here goes the depth first search: ...
301 views

### Hackerrank - Maximum Disjoint Subtree Product

Problem statement We define an unrooted tree, T, with the following properties: T is a connected graph with nodes ...
87 views

### Sort and describe itinerary segments

Given some incoming some data in JSON format, representing unsorted train, bus, and flight tickets: ...
8k views

### Java: Given a list of edges, build a tree and return the root

In an interview I was asked to write Java code to build a tree and return the root, given a list of edges. It was a fairly open ended question where the interviewer left all the decisions up to me. I ...
7k views

### Adjacency List implementation for Graph in Java

I am building up a Graph repository which I am planing to use for exploring all the Graph algorithms. This is more from a learning perspective only. I have implemented a basic ...
129 views

### Computing most reliable path in undirected probabilistic graphs using Java

(See the next iteration.) Problem definition We are given an undirected graph $G = (V, E)$ and a weight function $w \colon E \to (0, 1]$. The weight of the edge $e \in E$, $w(e)$, ...
116 views

### Calculating distances between nodes in graph with negative cycles

Full disclosure: this is for an online course. The code calculates the distances between a starting node in a graph and all other nodes using the Bellman-Ford algorithm. The graph may contain ...
247 views

### Most efficient implementation of Djisktra's shortest path using vectors and pairs in C++ stl

If std::vector<vector<pair<int,int> > > v(n) represents the adjacency list of the graph with ...
406 views

### CodeFights Quora bot

This isn't any homework questions or assignments, only my practice. In the CodeFights Quora bot. My code cannot pass even the first hidden input because of the execution time. I'm only a first-month ...
134 views

### Neo4j traversal algorithm to find dense nodes

I am trying to get familiar with traversals algorithms implementations in Neo4j and to practice I am implementing classic graph algorithms (not judging if is the best approach or not, just for the ...
3k views

### Undirected Unweighted Graph Implementation - C++

For a class we needed to implement an undirected unweighted graph that: Maintains a distance matrix that can be printed Supports the computation of the graph's diameter (uses dist matrix) Prints the ...
3k views

### Finding minimal spanning tree of a graph using Kruskal's algorithm

I am doing some practice interview questions for class, one of the questions is: Given an undirected graph G, find the minimum spanning tree. Function should take in and output an adjacency list. ...
341 views

### Lissajous pattern simulator

I had seen a YouTube video making actual Lissajous patterns with sand falling from a pendulum with 2 different periods and looking in the Wikipedia page the looked very easy to simulate. I made a ...
6k views

### MST Kruskal's algorithm in Python

I have this code for finding MST for undirected weighted graph, currently works for graphs with maximum 10 vertices. How can I update the code to scale for larger graphs? ...
2k views

### Depth and breadth first search

I am practicing interview questions, I wrote this code to perform BFS and DFS on a graph in Python. How can it be optimized, and how can it be made more readable? ...
2k views

### Undirected graph implementation

I have implemented in C# a graph data structure (undirected graph) using TDD technique, tests are provided upfront. This implementation provides common graph methods also it traverses graph using DFS ...
1k views

### Dijkstra implementation for maze solving that works fine and returns shortest path

I would like to make my code "prettier" and also speed up the runtime of my implementation. Nodeinfo is literally just an object that holds its "parent node"(the node it came from) and its distance. i ...
1k views

### Karger's min cut algorithm

I have implemented a simple version of Karger's min cut algorithm. The algorithm takes a graph and repeatedly contracts randomly selected edges until only two nodes are left. By repeating this ...
1k views

### Flight combinations between two cities

I am trying to obtain all possible ways to go from a city A to a city B on a given day by plane, with a maximum of 2 stops. I have as input a list of dictionaries: ...
365 views

### Kattis problem Amanda Lounges

I wrote this solution to Kattis problem Amanda Lounges in Scala. TL;DR: It's a graph theory problem where I read in a list of edges from stdin and try to compute the minimum number of nodes that ...
2k views

### Hacker Rank: Journey to the Moon (Graph Theory)

I am attempting to complete the "Journey to the Moon" problem described below: There are N trained astronauts numbered from 0 to N-1. But those in charge of the mission did not receive ...
174 views

### Java sequential BFS

I am currently working on optimizing my code. In order to do that, I have to make sure that my sequential BFS works perfectly. Then, I should I apply threads or executor service to run it in parallel. ...
186 views

### Is there a circular reference in a set of template substitutions?

I have a configuration engine that allows the user to specify a dictionary with text templates (read from a JSON file) where each placeholder like {x} can be ...
2k views

### Dijkstra's algorithm in JavaScript

I'm trying to implement Dijkstra's algorithm to find the shortest path. This is from a challenge on CodeWars. In this scenario there is a map: ...
547 views

### Simple Graph in Python

Just the beginning of graphs API in Python: ...
330 views

### Rock, Paper, Whatever - A small commandline game

My rational for this program, was to learn more of Python's standard library, I didn't actually know cmd was even in it until I started this program. It's also the ...
370 views

### Split DAG into disjoint sets

I wrote an algorithm to separate a DAG into disjoint sets of vertices. I was hoping to have some comments on algorithmic complexity and efficiency of the Python code. Notably, I don't like the adding ...
98 views

### Circle check algorithm optimization for directed graph

Here is my two versions of code to check if there is a circle for a directed graph. Any advice on performance improvements in terms of algorithm time complexity, code bugs or code style are highly ...
210 views

### Leetcode 210. Course Schedule II

Problem statement There are a total of $n$ courses you have to take, labeled from $0$ to $n - 1$. Some courses may have prerequisites, for example to take course $0$ you have to ...
44 views

### Modeling a control polygon for a piecewise spline curve

Aim I'm modeling a control polygon for a piecewise spline curve. Each sub-spline is defined by a location the spline must pass through, as well as a forward tangent, and a backwards tangent. The ...
1k views

### Representing an adjacency list (graph) using a hash table of lists

While writing a C++ implementation for weighed, labeled graphs, I started to doubt the effectiveness/correctness of my chosen structure and I would like to hear some reviews. I tried to implement the ...
387 views

### BFS in a grid with wall breaking saldo in Java

In this problem, we are given a 2-dimentional grid, with each cell being walkable or holding a wall. Given an integer $s \geq 0$, find the shortest path from the source node to target node breaking ...
410 views

### Second degree connection ranking

Problem Find 2nd degree connections ( friends’ friends), output these 2nd degree connections ranked by number of common friends (i.e 1st degree connections) with you, (example: if 2nd degree ...
301 views

### Minimum number of flight segments using breadth-first search

I'm practising with graphs, and trying to solve a problem of calculating the minimum number of flight segments, applying breadth-first search. The code is working, but I think, that it's not clean. ...
2k views

### Hackerrank - value of friendship (II)

Problem statement You're researching friendships between groups $n$ of new college students where each student is distinctly numbered from $1$ to $n$. At the beginning of the semester, no ...
1k views

### Eulerian Path algorithm

I'm doing a project to find the Eulerian path. Cane someone find an example where the algorithm is wrong? The function EulerianPath recursively prints the Eulerian ...
1k views

### Hackerank - value of friendship

Problem statement You're researching friendships between groups $n$ of new college students where each student is distinctly numbered from $1$ to $n$. At the beginning of the semester, no ...
427 views

### Tarjan's strongly connected component finding algorithm

Here is my code for Tarjan's strongly connected component algorithm. Please point out any bugs, performance/space (algorithm time/space complexity) optimization or code style issues. ...
1k views

### Implementation of a graph in Java

A long time ago I posted my implementation of directed graph is Java. Recently I took a look at @coderodde's implementation and decided to write an implementation of undirected graph and directed ...
188 views

### unordered_multimap approach to adjacency list and graph search

I'm looking for suggestions on how to improve the usage of modern C++ features with this simple graph search algorithm, particularly with regard to the use of STL containers and lambda expressions. ...
2k views

### Dijkstra's algorithm using priority queue running slower than without PQ

I need to implement dijkstra's algorithm and I've done so using this Wikipedia page. I've done it both with priority queue and without. Both versions work 100% correct, however I need the faster one ...
173 views

### Code to calculate minimum number of pushes required to put the whole dominoes set down

The question is this one on UVa, and it basically gives us n, the number of dominoes and (a,b) relations such that pushing ...
525 views

### Dijkstra's algorithm using a specific structure

I have implemented Dijkstra's algorithm using a slightly modified version of the structure and class posted here. Unfortunately, I have ruined the efficiency. I am intent on using this structure. I ...
617 views

### Graph representation implementation

I wanted to represent graphs in Python, and wrote the following class. I don't care about values associated with nodes or the weights of the connections between them, simply the connections between ...
3k views

### BFS' shortest path unweighted directed graph

The code I have is based on BFS and a little bit of Dijkstra and returns the shortest path of an unweighted directed graph as an integer. I was wondering if someone could take a look at my code too ...