图的用处也很广。搜索引擎的爬虫机器人在网上下载网页肯定用到了图这种数据结构,包括图的深度优先/广度优先算法,以及图中两个节点间的最短距离及
最短路径(路线)。本实现简单实现了图的这三个算法。
我这里用来表示图的数据结构包括节点和边(有向图,边含有权重)。
节点的实现如下:
public class Node {
private String name;
private boolean isVisited=false; //是否访问过
private boolean dis_centern=false; //到指定节点的最短距离是否已经确定
private int dis=0; //到指定点的最短距离
private String path=""; //存储指定点到该点的路径
public Node() {
}
public Node(String name) {
this.name = name;
}
public boolean isDis_centern() {
return dis_centern;
}
public void setDis_centern(boolean dis_centern) {
this.dis_centern = dis_centern;
}
public int getDis() {
return dis;
}
public void setDis(int dis) {
this.dis = dis;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isVisited() {
return isVisited;
}
public void setVisited(boolean isVisited) {
this.isVisited = isVisited;
}
}
边的实现如下:
public class Side {
private Node from; //边的出发节点
private Node to; //边的目的地节点
private int dis; //边的长度(权重)
public Side(Node from, Node to, int dis) {
this.from = from;
this.to = to;
this.dis = dis;
}
public Node getFrom() {
return from;
}
public void setFrom(Node from) {
this.from = from;
}
public Node getTo() {
return to;
}
public void setTo(Node to) {
this.to = to;
}
public int getDis() {
return dis;
}
public void setDis(int dis) {
this.dis = dis;
}
}