Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions src/main/java/edu/illinois/yasgl/DirectedGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,17 @@ of this software and associated documentation files (the "Software"), to deal
import java.util.Map;
import java.util.stream.Collectors;

import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;

public class DirectedGraph<V> extends AbstractGraph<V> {

private static final long serialVersionUID = -3303603645240328439L;

final ImmutableMultimap<V, V> forward;
final ImmutableMultimap<V, V> backward;
final Multimap<V, V> forward;
final Multimap<V, V> backward;
final Collection<V> vertices;

protected DirectedGraph(ImmutableMultimap<V, V> forward, Collection<V> vertices) {
this.forward = forward;
this.backward = this.forward.inverse();
this.vertices = vertices;
}

private DirectedGraph(ImmutableMultimap<V, V> forward, ImmutableMultimap<V, V> backward,
protected DirectedGraph(Multimap<V, V> forward, Multimap<V, V> backward,
Collection<V> vertices) {
this.forward = forward;
this.backward = backward;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/edu/illinois/yasgl/DirectedGraphBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void addVertex(V vertex) {
public DirectedGraph<V> build() {
ImmutableMultimap<V, V> multi = this.forward.build();

return new DirectedGraph<V>(multi, ImmutableSet.copyOf(vertices));
return new DirectedGraph<V>(multi, multi.inverse(), ImmutableSet.copyOf(vertices));
}

}
6 changes: 5 additions & 1 deletion src/main/java/edu/illinois/yasgl/Edge.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ of this software and associated documentation files (the "Software"), to deal

package edu.illinois.yasgl;

public class Edge<V> {
import java.io.Serializable;

public class Edge<V> implements Serializable {

private static final long serialVersionUID = -2193320769746243885L;

private V source;
private V destination;
Expand Down
23 changes: 5 additions & 18 deletions src/main/java/edu/illinois/yasgl/LabeledDirectedGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ of this software and associated documentation files (the "Software"), to deal
import java.util.Set;
import java.util.stream.Collectors;

import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSetMultimap;

Expand All @@ -44,27 +44,14 @@ public class LabeledDirectedGraph<V, E> extends AbstractGraph<V> implements Edge

private static final long serialVersionUID = 4772562024828519617L;

final ImmutableMultimap<V, VertexEntry<V, E>> forward;
final ImmutableMultimap<V, VertexEntry<V, E>> backward;
final Multimap<V, VertexEntry<V, E>> forward;
final Multimap<V, VertexEntry<V, E>> backward;
final Collection<V> vertices;

Collection<E> edges;

public LabeledDirectedGraph(ImmutableMultimap<V, VertexEntry<V, E>> multi,
ImmutableSet<V> vertices) {
this.forward = multi;
ImmutableMultimap.Builder<V, VertexEntry<V, E>> backBuilder = ImmutableSetMultimap
.builder();
for (Entry<V, VertexEntry<V, E>> entry : multi.entries()) {
backBuilder.put(entry.getValue().getVertex(),
new VertexEntry<>(entry.getKey(), entry.getValue().getEdge()));
}
this.backward = backBuilder.build();
this.vertices = vertices;
}

private LabeledDirectedGraph(ImmutableMultimap<V, VertexEntry<V, E>> forward,
ImmutableMultimap<V, VertexEntry<V, E>> backward, Collection<V> vertices) {
protected LabeledDirectedGraph(Multimap<V, VertexEntry<V, E>> forward,
Multimap<V, VertexEntry<V, E>> backward, Collection<V> vertices) {
this.forward = forward;
this.backward = backward;
this.vertices = vertices;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ of this software and associated documentation files (the "Software"), to deal

import java.util.Collection;
import java.util.HashSet;
import java.util.Map.Entry;
import java.io.Serializable;

import com.google.common.collect.ImmutableMultimap;
Expand Down Expand Up @@ -63,7 +64,14 @@ public synchronized void addVertex(V vertex) {
public LabeledDirectedGraph<V, E> build() {
ImmutableMultimap<V, VertexEntry<V, E>> multi = this.forward.build();

return new LabeledDirectedGraph<V, E>(multi, ImmutableSet.copyOf(vertices));
ImmutableMultimap.Builder<V, VertexEntry<V, E>> backBuilder = ImmutableSetMultimap
.builder();
for (Entry<V, VertexEntry<V, E>> entry : multi.entries()) {
backBuilder.put(entry.getValue().getVertex(),
new VertexEntry<>(entry.getKey(), entry.getValue().getEdge()));
}

return new LabeledDirectedGraph<V, E>(multi, backBuilder.build(), ImmutableSet.copyOf(vertices));
}

public static class VertexEntry<V, E> implements Serializable {
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/edu/illinois/yasgl/MutableDirectedGraph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package edu.illinois.yasgl;

import java.util.Collection;
import java.util.HashSet;

import com.google.common.collect.Multimap;

public class MutableDirectedGraph<V> extends DirectedGraph<V> implements MutableGraph<V> {

private static final long serialVersionUID = 1L;

protected MutableDirectedGraph(Multimap<V, V> forward, Multimap<V,V> backward, Collection<V> vertices) {
super(forward, backward, vertices);
}

@Override
public void addEdge(V v1, V v2) {
this.forward.put(v1, v2);
this.backward.put(v2, v1);
this.vertices.add(v1);
this.vertices.add(v2);
}

@Override
public void addVertex(V v) {
this.vertices.add(v);

}

@Override
public void removeEdge(V v1, V v2) {
this.forward.remove(v1, v2);
this.backward.remove(v2, v1);
}

@Override
public void removeVertex(V v) {
this.vertices.remove(v);
Collection<V> successors = new HashSet<V>(this.forward.get(v));
Collection<V> predecessors = new HashSet<V>(this.backward.get(v));

predecessors.stream().forEach(pre -> forward.remove(pre, v));
this.forward.removeAll(v);

successors.stream().forEach(suc -> backward.remove(suc, v));
this.backward.removeAll(v);
}

}
57 changes: 57 additions & 0 deletions src/main/java/edu/illinois/yasgl/MutableDirectedGraphBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package edu.illinois.yasgl;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import com.google.common.collect.HashMultimap;

public class MutableDirectedGraphBuilder<V> {
private final Collection<V> vertices = new HashSet<V>();
private final HashMultimap<V, V> forward = HashMultimap.create();
private final HashMultimap<V, V> backward = HashMultimap.create();

private boolean built = false;

public MutableDirectedGraphBuilder() {
}

public MutableDirectedGraphBuilder(DirectedGraph<V> g) {
this.vertices.addAll(g.vertices);
for (V pre : g.forward.keys()) {
for (V suc : g.forward.get(pre)) {
this.addEdge(pre, suc);
}
}
}

private void validate() {
if (built) {
throw new UnsupportedOperationException("The builder built the object; no changes are allowed here.");
}

}

public void addEdge(V vertex1, V vertex2) {
this.validate();
this.addVertex(vertex1);
this.addVertex(vertex2);
this.forward.put(vertex1, vertex2);
this.backward.put(vertex2, vertex1);
}

public void addVertex(V vertex) {
this.validate();
this.vertices.add(vertex);
}

public DirectedGraph<V> build() {
built = true;
return new DirectedGraph<V>(forward, backward, vertices);
}

public static <V> DirectedGraph<V> empty() {
return new DirectedGraph<V>(HashMultimap.create(), HashMultimap.create(), new HashSet<>());
}

}
33 changes: 33 additions & 0 deletions src/main/java/edu/illinois/yasgl/MutableGraph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* The MIT License (MIT)

Copyright (c) 2016 Alex Gyori

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package edu.illinois.yasgl;


public interface MutableGraph<V> extends Graph<V> {

public void addEdge(V v1, V v2);
public void addVertex(V v);
public void removeEdge(V v1, V v2);
public void removeVertex(V v);
}