Label Edge Example

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Label Edge Example

Patricia_Sim
Hi Everybody

I'm trying to create directed graph with label edge by jgrapht. I learn how to create by "LabeledEdges" example which source code shown below

public class LabeledEdges {
    private static final String friend = "friend";
    private static final String enemy = "enemy";

    public static void main(String[] args) {
        DirectedGraph<String, RelationshipEdge> graph =
            new DirectedMultigraph<String, RelationshipEdge>(
                    new ClassBasedEdgeFactory<String, RelationshipEdge>(RelationshipEdge.class));

        ArrayList<String> people = new ArrayList<String>();
        people.add("John");
        people.add("James");
        people.add("Sarah");
        people.add("Jessica");

        // John is everyone's friend
        for (String person : people) {
            graph.addVertex(person);
            graph.addEdge(people.get(0), person, new RelationshipEdge<String>(people.get(0), person, friend));
        }

        // Apparently James doesn't really like John
        graph.addEdge("James", "John", new RelationshipEdge<String>("James", "John", enemy));

        // Jessica is Sarah and James's friend
        graph.addEdge("Jessica", "Sarah", new RelationshipEdge<String>("Jessica", "Sarah", friend));
        graph.addEdge("Jessica", "James", new RelationshipEdge<String>("Jessica", "James", friend));

        // But Sarah doesn't really like James
        graph.addEdge("Sarah", "James", new RelationshipEdge<String>("Sarah", "James", enemy));

         for (RelationshipEdge edge : graph.edgeSet()) {
        if (edge.toString().equals("enemy")) {
            System.out.printf(edge.getV1()+"is an enemy of "+ edge.getV2()+"\n");
        } else if (edge.toString().equals("friend")) {
            System.out.printf( edge.getV1()+" is a friend of "+ edge.getV2()+"\n");
        }
    }
    }

    public static class RelationshipEdge<V> extends DefaultEdge {
        private V v1;
        private V v2;
        private String label;

        public RelationshipEdge(V v1, V v2, String label) {
            this.v1 = v1;
            this.v2 = v2;
            this.label = label;
        }

        public V getV1() {
            return v1;
        }

        public V getV2() {
            return v2;
        }

        public String toString() {
            return label;
        }
    }

However, when I run this example, it show the error message
        "Exception in thread "main" java.lang.IllegalArgumentException: loops not allowed
        at org.jgrapht.graph.AbstractBaseGraph.addEdge(AbstractBaseGraph.java:243)"

How can I do to run this example
Reply | Threaded
Open this post in threaded view
|

Re: Label Edge Example

Szabolcs Besenyei
Hi!

The problem here is that DirectedMultigraph doest not allow loops and in your first loop you are trying to do
graph.addEdge(people.get(0), person, new RelationshipEdge<String>(people.get(0), person, friend));
which translates to
graph.addEdge("John", "John", new RelationshipEdge<String>("John", "John", friend)); on the first iteration.
 
Your option here is to either use a DefaultDirectedGraph (a non-simple directed graph in which multiple edges between any two vertices are not permitted, but loops are) or rewrite your code so that it does not try to friend John with "himself".



2014-03-15 16:18 GMT+01:00 Patricia_Sim <[hidden email]>:
Hi Everybody

I'm trying to create directed graph with label edge by jgrapht. I learn how
to create by "LabeledEdges" example which source code shown below

public class LabeledEdges {
    private static final String friend = "friend";
    private static final String enemy = "enemy";

    public static void main(String[] args) {
        DirectedGraph<String, RelationshipEdge> graph =
            new DirectedMultigraph<String, RelationshipEdge>(
                    new ClassBasedEdgeFactory<String,
RelationshipEdge>(RelationshipEdge.class));

        ArrayList<String> people = new ArrayList<String>();
        people.add("John");
        people.add("James");
        people.add("Sarah");
        people.add("Jessica");

        // John is everyone's friend
        for (String person : people) {
            graph.addVertex(person);
            graph.addEdge(people.get(0), person, new
RelationshipEdge<String>(people.get(0), person, friend));
        }

        // Apparently James doesn't really like John
        graph.addEdge("James", "John", new RelationshipEdge<String>("James",
"John", enemy));

        // Jessica is Sarah and James's friend
        graph.addEdge("Jessica", "Sarah", new
RelationshipEdge<String>("Jessica", "Sarah", friend));
        graph.addEdge("Jessica", "James", new
RelationshipEdge<String>("Jessica", "James", friend));

        // But Sarah doesn't really like James
        graph.addEdge("Sarah", "James", new
RelationshipEdge<String>("Sarah", "James", enemy));

         for (RelationshipEdge edge : graph.edgeSet()) {
        if (edge.toString().equals("enemy")) {
            System.out.printf(edge.getV1()+"is an enemy of "+
edge.getV2()+"\n");
        } else if (edge.toString().equals("friend")) {
            System.out.printf( edge.getV1()+" is a friend of "+
edge.getV2()+"\n");
        }
    }
    }

    public static class RelationshipEdge<V> extends DefaultEdge {
        private V v1;
        private V v2;
        private String label;

        public RelationshipEdge(V v1, V v2, String label) {
            this.v1 = v1;
            this.v2 = v2;
            this.label = label;
        }

        public V getV1() {
            return v1;
        }

        public V getV2() {
            return v2;
        }

        public String toString() {
            return label;
        }
    }

However, when I run this example, it show the error message
        "Exception in thread "main" java.lang.IllegalArgumentException:
loops not allowed
        at org.jgrapht.graph.AbstractBaseGraph.addEdge(AbstractBaseGraph.java:243)"

How can I do to run this example



--
View this message in context: http://jgrapht-users.107614.n3.nabble.com/Label-Edge-Example-tp4024899.html
Sent from the jgrapht-users mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/13534_NeoTech
_______________________________________________
jgrapht-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users


------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/13534_NeoTech
_______________________________________________
jgrapht-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users
Reply | Threaded
Open this post in threaded view
|

Re: Label Edge Example

Joris Kinable
In reply to this post by Patricia_Sim
You are creating a self-loop which is not allowed in the type of graph you are using. This means that you are adding a directed edge from a vertex to itself. From the looks of it, your error is in this loop:

for (String person : people) {
            graph.addVertex(person);
            graph.addEdge(people.get(0), person, new
                RelationshipEdge<String>(people.get(0), person, friend));
}

You are iterating over all the persons in the list people. This list includes John. In the same loop you are also adding edges from John (people.get(0)) to all persons in the list people. As a conclusion you are adding a loop from John to itself. Simple solution:


for(String person: people){
  graph.addVertex(person);
  if(person != john){
     graph.addEdge(... );
  }
}

br,

Joris


On Sat, Mar 15, 2014 at 11:18 AM, Patricia_Sim <[hidden email]> wrote:
Hi Everybody

I'm trying to create directed graph with label edge by jgrapht. I learn how
to create by "LabeledEdges" example which source code shown below

public class LabeledEdges {
    private static final String friend = "friend";
    private static final String enemy = "enemy";

    public static void main(String[] args) {
        DirectedGraph<String, RelationshipEdge> graph =
            new DirectedMultigraph<String, RelationshipEdge>(
                    new ClassBasedEdgeFactory<String,
RelationshipEdge>(RelationshipEdge.class));

        ArrayList<String> people = new ArrayList<String>();
        people.add("John");
        people.add("James");
        people.add("Sarah");
        people.add("Jessica");

        // John is everyone's friend
        for (String person : people) {
            graph.addVertex(person);
            graph.addEdge(people.get(0), person, new
RelationshipEdge<String>(people.get(0), person, friend));
        }

        // Apparently James doesn't really like John
        graph.addEdge("James", "John", new RelationshipEdge<String>("James",
"John", enemy));

        // Jessica is Sarah and James's friend
        graph.addEdge("Jessica", "Sarah", new
RelationshipEdge<String>("Jessica", "Sarah", friend));
        graph.addEdge("Jessica", "James", new
RelationshipEdge<String>("Jessica", "James", friend));

        // But Sarah doesn't really like James
        graph.addEdge("Sarah", "James", new
RelationshipEdge<String>("Sarah", "James", enemy));

         for (RelationshipEdge edge : graph.edgeSet()) {
        if (edge.toString().equals("enemy")) {
            System.out.printf(edge.getV1()+"is an enemy of "+
edge.getV2()+"\n");
        } else if (edge.toString().equals("friend")) {
            System.out.printf( edge.getV1()+" is a friend of "+
edge.getV2()+"\n");
        }
    }
    }

    public static class RelationshipEdge<V> extends DefaultEdge {
        private V v1;
        private V v2;
        private String label;

        public RelationshipEdge(V v1, V v2, String label) {
            this.v1 = v1;
            this.v2 = v2;
            this.label = label;
        }

        public V getV1() {
            return v1;
        }

        public V getV2() {
            return v2;
        }

        public String toString() {
            return label;
        }
    }

However, when I run this example, it show the error message
        "Exception in thread "main" java.lang.IllegalArgumentException:
loops not allowed
        at org.jgrapht.graph.AbstractBaseGraph.addEdge(AbstractBaseGraph.java:243)"

How can I do to run this example



--
View this message in context: http://jgrapht-users.107614.n3.nabble.com/Label-Edge-Example-tp4024899.html
Sent from the jgrapht-users mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/13534_NeoTech
_______________________________________________
jgrapht-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users


------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/13534_NeoTech
_______________________________________________
jgrapht-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users
Reply | Threaded
Open this post in threaded view
|

Re: Label Edge Example

Patricia_Sim
In reply to this post by Szabolcs Besenyei
Thank you very much

Patricia


On Thu, Mar 20, 2014 at 4:29 AM, Szabolcs Besenyei [via jgrapht-users] <[hidden email]> wrote:
Hi!

The problem here is that DirectedMultigraph doest not allow loops and in your first loop you are trying to do
graph.addEdge(people.get(0), person, new RelationshipEdge<String>(people.get(0), person, friend));
which translates to
graph.addEdge("John", "John", new RelationshipEdge<String>("John", "John", friend)); on the first iteration.
 
Your option here is to either use a DefaultDirectedGraph (a non-simple directed graph in which multiple edges between any two vertices are not permitted, but loops are) or rewrite your code so that it does not try to friend John with "himself".



2014-03-15 16:18 GMT+01:00 Patricia_Sim <[hidden email]>:

Hi Everybody

I'm trying to create directed graph with label edge by jgrapht. I learn how
to create by "LabeledEdges" example which source code shown below

public class LabeledEdges {
    private static final String friend = "friend";
    private static final String enemy = "enemy";

    public static void main(String[] args) {
        DirectedGraph<String, RelationshipEdge> graph =
            new DirectedMultigraph<String, RelationshipEdge>(
                    new ClassBasedEdgeFactory<String,
RelationshipEdge>(RelationshipEdge.class));

        ArrayList<String> people = new ArrayList<String>();
        people.add("John");
        people.add("James");
        people.add("Sarah");
        people.add("Jessica");

        // John is everyone's friend
        for (String person : people) {
            graph.addVertex(person);
            graph.addEdge(people.get(0), person, new
RelationshipEdge<String>(people.get(0), person, friend));
        }

        // Apparently James doesn't really like John
        graph.addEdge("James", "John", new RelationshipEdge<String>("James",
"John", enemy));

        // Jessica is Sarah and James's friend
        graph.addEdge("Jessica", "Sarah", new
RelationshipEdge<String>("Jessica", "Sarah", friend));
        graph.addEdge("Jessica", "James", new
RelationshipEdge<String>("Jessica", "James", friend));

        // But Sarah doesn't really like James
        graph.addEdge("Sarah", "James", new
RelationshipEdge<String>("Sarah", "James", enemy));

         for (RelationshipEdge edge : graph.edgeSet()) {
        if (edge.toString().equals("enemy")) {
            System.out.printf(edge.getV1()+"is an enemy of "+
edge.getV2()+"\n");
        } else if (edge.toString().equals("friend")) {
            System.out.printf( edge.getV1()+" is a friend of "+
edge.getV2()+"\n");
        }
    }
    }

    public static class RelationshipEdge<V> extends DefaultEdge {
        private V v1;
        private V v2;
        private String label;

        public RelationshipEdge(V v1, V v2, String label) {
            this.v1 = v1;
            this.v2 = v2;
            this.label = label;
        }

        public V getV1() {
            return v1;
        }

        public V getV2() {
            return v2;
        }

        public String toString() {
            return label;
        }
    }

However, when I run this example, it show the error message
        "Exception in thread "main" java.lang.IllegalArgumentException:
loops not allowed
        at org.jgrapht.graph.AbstractBaseGraph.addEdge(AbstractBaseGraph.java:243)"

How can I do to run this example



--
View this message in context: http://jgrapht-users.107614.n3.nabble.com/Label-Edge-Example-tp4024899.html
Sent from the jgrapht-users mailing list archive at Nabble.com.

------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/13534_NeoTech
_______________________________________________
jgrapht-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users


------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/13534_NeoTech
_______________________________________________
jgrapht-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users



If you reply to this email, your message will be added to the discussion below:
http://jgrapht-users.107614.n3.nabble.com/Label-Edge-Example-tp4024899p4024901.html
To unsubscribe from Label Edge Example, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

Re: Label Edge Example

Rikless
Hi,

I come back on this post because i had the same problem on an old algorithm using a DirectedWeightedMultigraph structure.

I know that there is loops on my data, that's why i choosed this structure.
As mentionned in the javadoc :
DirectedMultigraph (used by Patricia) : A directed multigraph is a non-simple directed graph in which loops and multiple edges between any two vertices are permitted.
DirectedWeightedMultigraph : A directed weighted multigraph is a non-simple directed graph in which loops and multiple edges between any two vertices are permitted, and edges have weights.

I think something have changed since version 0.9.0.

Any idea ?
What is the structure i can use with loops and multiple edges ?

Rik

Reply | Threaded
Open this post in threaded view
|

Re: Label Edge Example

Szabolcs Besenyei
Hi!

A multidigraph by definition is a directed graph which is permitted to have multiple edges with the same source and target nodes.
It can also allow loops, however some like to call these pseudographs, reserving the term multigraph for the case with no loops.
Jgrapht is implemented to preseve this distinction.

So to answer your question in short, DirectedPseudograph is the structure you are looking for.

Szabolcs


2014-04-01 0:06 GMT+02:00 Rikless <[hidden email]>:
Hi,

I come back on this post because i had the same problem on an old algorithm
using a DirectedWeightedMultigraph structure.

I know that there is loops on my data, that's why i choosed this structure.
As mentionned in the javadoc :
*DirectedMultigraph* (used by Patricia) : A directed multigraph is a
non-simple directed graph in which loops and multiple edges between any two
vertices are permitted.
*DirectedWeightedMultigraph* : A directed weighted multigraph is a
non-simple directed graph in which loops and multiple edges between any two
vertices are permitted, and edges have weights.

I think something have changed since version 0.9.0.

Any idea ?
What is the structure i can use with loops and multiple edges ?

Rik





--
View this message in context: http://jgrapht-users.107614.n3.nabble.com/Label-Edge-Example-tp4024899p4024909.html
Sent from the jgrapht-users mailing list archive at Nabble.com.

------------------------------------------------------------------------------
_______________________________________________
jgrapht-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users


------------------------------------------------------------------------------

_______________________________________________
jgrapht-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users
Reply | Threaded
Open this post in threaded view
|

Re: Label Edge Example

Sebastian Müller
In reply to this post by Rikless
I believe there was a discussion about this last year.

See change here:
https://github.com/jgrapht/jgrapht/commit/8fe299c38648acac55e40bd47498a08269e01f74

You can use a DirectedPseudograph in your case.
http://jgrapht.org/javadoc/index.html?org/jgrapht/graph/DirectedWeightedPseudograph.html


Sebastian


On 04/01/2014 12:06 AM, Rikless wrote:

> Hi,
>
> I come back on this post because i had the same problem on an old algorithm
> using a DirectedWeightedMultigraph structure.
>
> I know that there is loops on my data, that's why i choosed this structure.
> As mentionned in the javadoc :
> *DirectedMultigraph* (used by Patricia) : A directed multigraph is a
> non-simple directed graph in which loops and multiple edges between any two
> vertices are permitted.
> *DirectedWeightedMultigraph* : A directed weighted multigraph is a
> non-simple directed graph in which loops and multiple edges between any two
> vertices are permitted, and edges have weights.
>
> I think something have changed since version 0.9.0.
>
> Any idea ?
> What is the structure i can use with loops and multiple edges ?
>
> Rik
>
>
>
>
>
> --
> View this message in context: http://jgrapht-users.107614.n3.nabble.com/Label-Edge-Example-tp4024899p4024909.html
> Sent from the jgrapht-users mailing list archive at Nabble.com.
>
> ------------------------------------------------------------------------------
> _______________________________________________
> jgrapht-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/jgrapht-users


------------------------------------------------------------------------------
_______________________________________________
jgrapht-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users
Reply | Threaded
Open this post in threaded view
|

Re: Label Edge Example

Sebastian Müller
In reply to this post by Szabolcs Besenyei
Wolfram MathWorld explains it well:

" Some references require that multigraphs possess no graph loops, some explicitly allow them, and yet others do not include any explicit allowance or disallowance. Worse still, Tutte uses the term "multigraph" to mean a graph containing either loops or multiple edges. As a result of these many ambiguities, use of the term "multigraph" should be deprecated, or at the very least used with extreme caution. "

http://mathworld.wolfram.com/Multigraph.html


Sebastian


On 04/01/2014 01:54 PM, Szabolcs Besenyei wrote:
Hi!

A multidigraph by definition is a directed graph which is permitted to have multiple edges with the same source and target nodes.
It can also allow loops, however some like to call these pseudographs, reserving the term multigraph for the case with no loops.
Jgrapht is implemented to preseve this distinction.

So to answer your question in short, DirectedPseudograph is the structure you are looking for.

Szabolcs


2014-04-01 0:06 GMT+02:00 Rikless <[hidden email]>:
Hi,

I come back on this post because i had the same problem on an old algorithm
using a DirectedWeightedMultigraph structure.

I know that there is loops on my data, that's why i choosed this structure.
As mentionned in the javadoc :
*DirectedMultigraph* (used by Patricia) : A directed multigraph is a
non-simple directed graph in which loops and multiple edges between any two
vertices are permitted.
*DirectedWeightedMultigraph* : A directed weighted multigraph is a
non-simple directed graph in which loops and multiple edges between any two
vertices are permitted, and edges have weights.

I think something have changed since version 0.9.0.

Any idea ?
What is the structure i can use with loops and multiple edges ?

Rik





--
View this message in context: http://jgrapht-users.107614.n3.nabble.com/Label-Edge-Example-tp4024899p4024909.html
Sent from the jgrapht-users mailing list archive at Nabble.com.

------------------------------------------------------------------------------
_______________________________________________
jgrapht-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users



------------------------------------------------------------------------------


_______________________________________________
jgrapht-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users


------------------------------------------------------------------------------

_______________________________________________
jgrapht-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users
Reply | Threaded
Open this post in threaded view
|

Re: Label Edge Example

Rikless
Thanks a lot !