@LIUHUAN
2019-09-03T13:12:42.000000Z
字数 4657
阅读 865
algorithm
标签(空格分隔): algorithm
void traveralGraph(){
初始化标志hashmap
for node in G: // 对图中的每一个节点
如果没有被访问:hashmap[node] == false
执行DFS DFS(G,node,hashmap)
}
void DFS(G,node,hashmap){
// 递归的出口
如果节点被访问了:if hashmap[node] == true:
返回: return
1.访问节点:visited(node)
2.标志节点已被访问:hashmap[node] = true
3.获取node节点的旁边,邻接节点集合:adjlist = getadjList(node)
for adjNode : adjlist: 对每一个邻接点,执行
1).如果邻接点没有被访问:hashmap[adjNode] != true
2).以这个节点开始,进行DFS遍历 :DFS(G,adjNode,hashmap)
}
}
A --- B --- C
|
D
|
E
| | | | | C | | | | D | | | | E | | |
| | --> | B | -> | B | -> | B | | B | | B | | B | |B|
| A | | A | | A | | A | | A | | A | | A | |A|
—————
DFS(A) ->
DFS(B) -> for n in (D,C)
DFS(C)
DFS(D) DFS(C)返回
DFS(D)
DFS(D)返回
DFS(E)
DFS(E)返回
DFS(B)返回
DFS(A)返回
public class DFSMethod {
public static void main(String[] args){
G = new Graph();
hashmap = new hashmap();
for node in G:
if hashmap[node] == false:
DFS(G,node,hashmap);// 表示从0 节点开始遍历
}
public static void BFS(G,node,hashmap) {
//1.已经认为是在队列当中,说明已经要被访问了,所以这么标志
visitedMap.put(node, true);
//2.访问
visited(node);
// 3.获取相邻的节点
ArrayList<Node> adjNodeList = G.getAdjectList(q);
// 4.开始以邻接点为七点执行DFS搜索
for(int i = 0; i < adjNodeList.size() ; ++i) {
node = adjNodelist.get(i)
/**没有被访问过,则以这个节点开始执行DFS搜索*/
if (visitedMap[node] == false) { // 没有被访问过
DFS(g,node,hashmap);
}
}//end for
}
public static void visited(Node s) {
// 这里只是简单的打印一下,可进行性统计、计数等操作
System.out.println(s);
}
}
[[0,0,1,0,0,0,0,1,0,0,0,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[0,1,1,0,1,0,0,0,0,0,0,0,0],
[0,1,0,0,1,1,0,0,1*,0, 1*,0,0],
[0,1,0,0,1,1,0,0,1*,1*,1*,0,0],
[0,0,0,0,0,0,0,0,0,0,1*,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[0,0,0,0,0,0,0,1,1,0,0,0,0]]
上述中有个连通分量(带*的1),大小为6
/***
* x,y 坐标的相邻坐标
* (x-1,y-1)---(x-1,y)----(x,y-1)
* (x , y-1)---(x , y)----(x,y+1)
* (x+1,y-1)---(x+1,y)----(x+1,y+1)
*/
public static int visitedCount = 0;
public static int maxAreaOfIsland(int[][] grid) {
//访问标志
HashMap<String,Boolean> visitedMap = new HashMap<String,Boolean>();
int count = 0;
for(int x =0; x < grid.length; ++x) {
for(int y = 0; y < grid[0].length; ++y) {
if(grid[x][y] == 0)// 跳过非连通节点
continue;
String xykey = x + ":" + y;
if(visitedMap.containsKey(xykey) == false) {
DFS(grid,visitedMap,x,y);// 把连通的岛遍历一下,并标志下,visitedCount记录这个连通分量节点数
count = count > visitedCount ? count : visitedCount;
visitedCount = 0;// 下一个连通分量遍历
}
}
}
return count;
}
public static void DFS(int[][] grid,HashMap<String,Boolean> visitedMap,int startx,int starty) {
NodeValue e = new NodeValue(startx, starty);
//访问标志
visitedMap.put(startx + ":" + starty, true);
visited(e);
/*获取邻接点集合*/
ArrayList<NodeValue> adjlist = getAdjList(grid,e);
// 邻接点调用DFS
for(int i = 0 ;i < adjlist.size(); ++i) {
e = adjlist.get(i);
String ekey = e.x +":" + e.y;
if(visitedMap.containsKey(ekey) == false) {
DFS(grid,visitedMap,e.x,e.y);
}
}
}
private static void visited(NodeValue e) {
visitedCount ++;// 访问到的节点数增加,计数功能
}
/**寻找其相邻节点的函数*/
public static ArrayList<NodeValue> getAdjList(int[][] grid,NodeValue e) {
ArrayList<NodeValue> adjlist = new ArrayList<NodeValue>();
int xlen = grid.length;
int ylen = grid[0].length;
if(e.x - 1 >=0 ) { // 上面(x-1,y)
if (grid[e.x -1][e.y] == 1) {
adjlist.add(new NodeValue(e.x - 1,e.y));
}
}
if(e.y - 1 >=0 ) { //左面(x,y-1)
if (grid[e.x][e.y - 1 ] == 1) {
adjlist.add(new NodeValue(e.x,e.y - 1 ));
}
}
if(e.x + 1 < xlen ) { // 下面 (x+1,y)
if (grid[e.x + 1][e.y] == 1) {
adjlist.add(new NodeValue(e.x + 1,e.y));
}
}
if(e.y + 1 < ylen ) { // 右面(x,y+1)
if (grid[e.x ][e.y + 1] == 1) {
adjlist.add(new NodeValue(e.x,e.y + 1));
}
}
return adjlist;
}
/**存储下x,y的坐标*/
static class NodeValue{
public int x, y;
public NodeValue(int x,int y){
this.x = x;
this.y = y;
}
}