-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVertex.java
82 lines (74 loc) · 1.77 KB
/
Vertex.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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package graph;
import java.util.*;
/**
*
* @author Delaney Sylvans
*/
public class Vertex {
public String name;
protected Set<Edge> edges;
public Vertex(String name){
this.name = name;
this.edges = new HashSet<>();
}
/**
* Gets the vertex name
*
* @return <tt>String</tt> name
*/
public String getName(){
return this.name;
}
/**
* Adds an outgoing edge to the vertex
*
* @param to
*
* @param weight
*
* @return <tt>true</tt> if the edge was added successfully
*/
public boolean addEdge(String to, int weight){
if (weight < 1){
return false;
}
boolean exists = false;
for (Edge edge : edges) {
if (edge.destination == to){
exists = true;
break;
}
}
if (exists) return false;
else {
Edge edge = new Edge(this.name, to, weight);
this.edges.add(edge);
return true;
}
}
/**
* Prints a list of all outgoing edges
*/
public void printEdges(){
String list = "";
Iterator<Edge> i = this.edges.iterator();
while (i.hasNext()){
Edge e = i.next();
list += "To: " + e.destination + ", Weight: " + e.weight + "\n";
}
System.out.println(list);
}
/**
* Gets the number of outgoing edges
*
* @return number of outgoing edges
*/
public int noOfEdges(){
return this.edges.size();
}
}