-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGraph.java
149 lines (123 loc) · 3.49 KB
/
Graph.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package Incognito;
import java.util.ArrayList;
/**
*
* @author adenugad
*/
public class Graph {
private final ArrayList<Vertex> vertices = new ArrayList<>();
private final ArrayList<Edge> edges = new ArrayList<>();
public Graph(){
}
public ArrayList<Vertex> getVertices() {
return vertices;
}
public ArrayList<Edge> getEdges() {
return edges;
}
public Graph copy(){
Graph graph = new Graph();
for(int i = 0; i < vertices.size(); i++ ){
graph.vertices.add(vertices.get(i));
}
for(int i = 0; i < edges.size(); i++ ){
graph.edges.add(edges.get(i));
}
return graph;
}
public int numEdges(){
return edges.size();
}
public int numVertices(){
return vertices.size();
}
public void addEdge(Vertex x, Vertex y){
Edge edge = new Edge(x, y);
edges.add(edge);
x.addIncidentEdges(edge);
//y.addIncidentEdges(edge);
}
public void addEdge(Edge edge){
if(edges.contains(edge) == false)
{
edges.add(edge);
}
}
public void addVertex(Vertex v){
if(vertices.contains(v) == false)
{
vertices.add(v);
}
}
public void removeEdge(Edge e){
if(edges.contains(e)){
edges.remove(e);
}
e.from.getIncidentEdges().remove(e);
//e.to.getIncidentEdges().remove(e);
}
public void removeVertex(Vertex v){
vertices.remove(v);
for(int i = 0; i < v.getNumOfIncidentEdges(); i++){
removeEdge(v.getIncidentEdges().get(i));
}
}
public Vertex getVertex(int index){
return vertices.get(index);
}
public void printOut(){
//System.out.println(vertices.size());
for(int i = 0; i < vertices.size(); i++){
for(int j = 0; j < vertices.get(i).getNumOfIncidentEdges(); j++){
System.out.println(vertices.get(i).getData() + " -> "
+ vertices.get(i).getIncidentEdges().get(j).getAdjacentVertex(vertices.get(i)).getData());
}
System.out.println();
}
}
/**
* Does graph have vertex ?
* @param v - vertex
* @return vertex if it's in graph
*/
public Vertex hasVertex(Vertex v){
for(int i = 0; i < vertices.size(); i++){
if(vertices.get(i).equals(v)){
return vertices.get(i);
}
}
return v;
}
/**
* Does graph have vertex ?
* @param v - vertex
* @return
*/
public boolean hasVertex2(Vertex v){
for(int i = 0; i < vertices.size(); i++){
if(vertices.get(i).equals(v)){
return true;
}
}
return false;
}
/**
* all nodes(vertices) in graph with no edge directed to them
* @return list of these vertices
*/
public ArrayList<Vertex> getRoots(){
ArrayList<Vertex> queue = new ArrayList<>();
for(int j = 0; j < vertices.size(); j++){
boolean issaRoot = false;
for(int i = 0; i < edges.size(); i++){
if(vertices.get(j).equals(edges.get(i).to)){
issaRoot = true;
}
}
if(issaRoot == false){
queue.add(vertices.get(j));
}
}
return queue;
}
}