question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Outgoing and incoming edges in undirected graphs

See original GitHub issue

A number of algorithms, defined on both directed and undirected graphs, iterate over the outgoing or incoming edges of a graph. This creates some problems, because in an UndirectedGraph, you cannot iterate over the outgoingEdgesOf(V vertex). Instead, you have to iterate over edgesOf(V vertex). A number of implementations (e.g. BidirectionalDijkstraShortestPath) try to resolve this issue as follows:

abstract class Specifics{
            public abstract Set<? extends E> edgesOf(V vertex);
        }

        class DirectedSpecifics extends Specifics{

            private DirectedGraph<V, E> graph;

            public DirectedSpecifics(DirectedGraph<V, E> g){
                graph = g;
            }

            @Override
            public Set<? extends E> edgesOf(V vertex){
                return graph.outgoingEdgesOf(vertex);
            }
        }

        class UndirectedSpecifics extends Specifics{
            private Graph<V, E> graph;

            public UndirectedSpecifics(Graph<V, E> g){
                graph = g;
            }

            @Override
            public Set<E> edgesOf(V vertex){
                return graph.edgesOf(vertex);
            }
        }

This is becoming a bit of a pattern throughout the library, so we probably want to address this. I don’t immediately have a clean solution (requires some more thought so determine the consequences of different solutions). Here are 4 different possibilities:

  1. Add default functions outgoingEdgesOf and incomingEdgesOf to the Graph interface, which implements the above behavior.
  2. Add a dedicated method to the utility class Graphs.
  3. Add this behavior to the UndirectedGraph interface only (this would only solve the problem partially)
  4. Create a special GraphDelegator implementation. Perhaps the easiest, least invasive, but probably also the least intuitive one.

Opinions are welcome. Alternatively, propose a solution through a pull request and we’ll have a look at it.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
jsichicommented, Sep 26, 2016

I don’t think anything prevents us from creating a custom implementation of the Set interface if we want to eliminate the copying.

BTW for the new EdgeAccessor we’re contemplating here, I’m fine with making it produce an Iterable rather than a Set, since the intention is to provide a way to enumerate edges during traversal (as opposed to something like edgesOf, which is intended to define the data structure).

0reactions
d-michailcommented, Mar 13, 2017

Solved in #366.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Lecture 24: Graph Representations and Traversals - Cornell CS
The Graph module in graph.ml implements this signature using adjacency lists of vertices, where both the outgoing edge list and the incoming edge...
Read more >
The same outgoing and incoming degree in graph
I have an undirected graph with n vertices and m edges. How to determinate in poly(n,m), is it possible (and how is it...
Read more >
Graph Terminology
Outgoing edges of a vertex are directed edges that the vertex is the origin. Incoming edges of a vertex are directed edges that...
Read more >
Count number of edges in an undirected graph - GeeksforGeeks
Write a function to count the number of edges in the undirected graph. Expected time complexity : O(V). Examples: Input : Adjacency list ......
Read more >
Data Structures - Graphs - Dimitrios Michail
An undirected graph G is a pair of sets (V, E) where V is a set of nodes ... Sometimes we would also...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found