Skip to content

Instantly share code, notes, and snippets.

@swapnilraj
Created November 3, 2017 15:11
Show Gist options
  • Save swapnilraj/11429880a30f9b56439e6fdd66137297 to your computer and use it in GitHub Desktop.
Save swapnilraj/11429880a30f9b56439e6fdd66137297 to your computer and use it in GitHub Desktop.
Implementation of a Graph in Java. Testing with different kinds of implementation.
// Copyright 2017 Swapnil Raj<sr@sraj.me>
//
// 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.
import java.util.*;
class Graph {
Node root;
Graph(int weight) {
root = new Node(weight);
}
private class Node {
ArrayList<Node> children;
int weight;
boolean used = false;
Node() {
children = new ArrayList<Node>();
}
Node(int weight) {
this.weight = weight;
}
Node(int weight, Node child) {
children = new ArrayList<Node>();
child.add(child);
this.weight = weight;
}
}
Node getRoot() {
return root;
}
Node findByWeight(int weight) {
if(root.weight == weight) {
return root;
}
if(root.children.length == 0) {
return null;
}
for(Node n : root.children) {
return n.findByWeight(weight);
}
}
String search(int weight) {
if(this.weight == weight) {
return root;
}
if(root.children.length == 0) {
return null;
}
for(Node n : root.children) {
return root.weight + "" + n.findByWeight(weight);
}
}
void addChild(Node n) {
children.add(n);
}
}
// Copyright 2017 Swapnil Raj<sr@sraj.me>
//
// 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.
import java.util.ArrayList;
import java.util.HashMap;
class GraphList1 {
private class Vertex {
int data;
boolean searched;
Vertex(int data) {
this.data = data;
}
public int getValue() {
return this.data;
}
public boolean equals(Vertex n) {
return getValue() == n.getValue();
}
public String toString() {
return data + "";
}
}
private class Edge {
HashMap<Vertex, Vertex> edge;
Edge() {
edge = new HashMap<Vertex, Vertex>();
}
Edge(Vertex one, Vertex two) {
this();
edge.put(one, two);
edge.put(two, one);
}
public boolean equals(Edge e) {
for(Vertex key : this.edge.keySet()) {
if(e.edge.containsKey(key)) {
if(edge.get(key).getValue() != e.edge.get(key).getValue() ) {
return false;
}
} else {
return false;
}
}
return true;
}
public String toString() {
String s = "";
for(Vertex key : this.edge.keySet()) {
s += key + "<->" + edge.get(key) + "\n";
}
return s;
}
}
private class Graph {
ArrayList<Vertex> vertices;
ArrayList<Edge> edges;
Graph() {
vertices = new ArrayList<Vertex>();
edges = new ArrayList<Edge>();
}
Graph(Edge e) {
this();
edges.add(e);
// for(Vertex key : e.edge.keySet()) {
// addVertex(key);
// addVertex(e.edge.get(key));
// }
}
Graph(Vertex v) {
this();
vertices.add(v);
}
Graph(Vertex v, Edge e) {
this();
edges.add(e);
vertices.add(v);
}
public void addEdge(Edge e) {
edges.add(e);
// for(Vertex key : e.edge.keySet()) {
// addVertex(key);
// addVertex(e.edge.get(key));
// }
}
public void addVertex(Vertex v) {
vertices.add(v);
}
public void printGraph() {
System.out.println("Vertices :-");
for(Vertex v : vertices) {
System.out.println(v);
}
System.out.println("Edges :-");
for(Edge e : edges) {
System.out.println(e);
}
}
}
public static void main(String[] args) {
GraphList1 o = new GraphList1();
Graph g = o.new Graph();
Vertex root = o.new Vertex(0);
g.addEdge(o.new Edge(root, o.new Vertex(1)));
g.addEdge(o.new Edge(root, o.new Vertex(5)));
g.addEdge(o.new Edge(o.new Vertex(1), o.new Vertex(2)));
g.addEdge(o.new Edge(o.new Vertex(2), o.new Vertex(3)));
g.addEdge(o.new Edge(o.new Vertex(3), o.new Vertex(4)));
g.addVertex(o.new Vertex(0));
g.addVertex(o.new Vertex(1));
g.addVertex(o.new Vertex(2));
g.addVertex(o.new Vertex(3));
g.addVertex(o.new Vertex(4));
g.addVertex(o.new Vertex(5));
g.printGraph();
}
}
// Copyright 2017 Swapnil Raj<sr@sraj.me>
//
// 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.
import java.util.HashMap;
import java.util.LinkedList;
class GraphList2 {
private class Vertex<T extends Comparable<T>> {
private T data;
private boolean visited;
Vertex() {
visited = false;
}
Vertex(T data) {
this();
this.data = data;
}
Vertex(T data, boolean visited) {
this();
this.data = data;
this.visited = visited;
}
@Override
public boolean equals(Object o) {
if(o == null) {
return false;
}
if(!(o instanceof Vertex<?>)) {
return false;
}
final Vertex<?> other = (Vertex<?>) o;
return this.data == other.getValue();
}
@Override
public String toString() {
return data.toString();
}
@Override
public int hashCode() {
return data.hashCode();
}
public boolean wasVisited() {
return visited;
}
public T getValue() {
return this.data;
}
}
private class Graph<T extends Comparable<T>> {
HashMap<Vertex<T>, LinkedList<Vertex<T>>> adjacencyList;
Graph() {
adjacencyList = new HashMap<Vertex<T>, LinkedList<Vertex<T>>>();
}
public void addChild(Vertex<T> src, Vertex<T> dst) {
LinkedList<Vertex<T>> neighbours = adjacencyList.get(src);
if(neighbours != null) {
neighbours.add(dst);
} else {
neighbours = new LinkedList<Vertex<T>>();
neighbours.add(dst);
adjacencyList.put(src, neighbours);
}
}
public void printGraph() {
for(Vertex<T> neighbours : adjacencyList.keySet()) {
System.out.print(neighbours);
System.out.print(" -> " + adjacencyList.get(neighbours));
System.out.print("\n");
}
}
}
public static void main(String[] args) {
GraphList2 o = new GraphList2();
Graph<Integer> g = o.new Graph<Integer>();
g.addChild(o.new Vertex<Integer>(3), o.new Vertex<Integer>(4));
g.addChild(o.new Vertex<Integer>(7), o.new Vertex<Integer>(9));
g.addChild(o.new Vertex<Integer>(3), o.new Vertex<Integer>(2));
g.printGraph();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment