图遍历算法

本文介绍了图的顶点和边的类定义,详细讲解了深度优先遍历(DFS)和广度优先遍历(BFS)算法,以及它们在Java中的实现,包括递归和非递归版本。
摘要由CSDN通过智能技术生成

        图的遍历算法有两种:深度优先遍历、广度优先遍历算法。

        深度优先遍历算法就是从起始结点开始,只要有相邻且未被访问的结点就要直接进行访问,直到最后不能向下遍历为止,再回溯寻找下一个策略。

        广度优先遍历算法,就是从起点开始向下一层一层的进行遍历,直到最后没有结点。

一、图的结点和边的相关类定义

        顶点:

package graph;

import java.util.List;

/**
 * 顶点类
 */
public class Vertex {
    String name;
    List<Edge> edges;
    boolean flag;//是否被访问过
    int inDegree;//结点的入度

    public Vertex() {
    }

    public Vertex(String name, List<Edge> edges) {
        this.name = name;
        this.edges = edges;
    }

    public Vertex(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Edge> getEdges() {
        return edges;
    }

    public void setEdges(List<Edge> edges) {
        this.edges = edges;
    }
}

        边:

package graph;

/**
 * 边类
 */
public class Edge {
    Vertex vertex;
    int weight;

    public Edge() {
    }

    public Edge(Vertex vertex) {
        this.vertex = vertex;
    }

    public Edge(Vertex vertex, int weight) {
        this.vertex = vertex;
        this.weight = weight;
    }

    public Vertex getVertex() {
        return vertex;
    }

    public void setVertex(Vertex vertex) {
        this.vertex = vertex;
    }

    public int getWeight() {
        return weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }
}

  二、深度优先遍历算法

package graph;

import java.util.LinkedList;
import java.util.List;

public class DFS {
    public static void main(String[] args) {
        Vertex v1=new Vertex("v1",new LinkedList<>());
        Vertex v2=new Vertex("v2",new LinkedList<>());
        Vertex v3=new Vertex("v3",new LinkedList<>());
        Vertex v4=new Vertex("v4",new LinkedList<>());
        Vertex v5=new Vertex("v5",new LinkedList<>());
        Vertex v6=new Vertex("v6",new LinkedList<>());

        v1.edges.add(new Edge(v3,9));
        v1.edges.add(new Edge(v2,7));
        v1.edges.add(new Edge(v6,14));

        v2.edges.add(new Edge(v4,15));

        v3.edges.add(new Edge(v4,11));
        v3.edges.add(new Edge(v6,2));

        v4.edges.add(new Edge(v5,6));

        v6.edges.add(new Edge(v5,9));

//        dfs(v1);
        dfs2(v1);
    }
    //递归方式从一个结点开始深度遍历整个图
    public static void dfs(Vertex v){
        v.flag=true;
        System.out.println(v.name);
        for(Edge edge:v.edges){
            if(!edge.vertex.flag){
                dfs(edge.vertex);
            }
        }
    }
    //非递归方式从一个结点开始深度遍历整个图
    public static void dfs2(Vertex vertex){
        LinkedList<Vertex> stack=new LinkedList<>();
        stack.push(vertex);
        while (!stack.isEmpty()){
            Vertex pop = stack.pop();
            pop.flag=true;
            System.out.println(pop.name);
            for (Edge edge : pop.edges) {
                if(!edge.vertex.flag){
                    stack.push(edge.vertex);
                }
            }
        }
    }
}

三、广度优先遍历算法

package graph;

import java.util.LinkedList;
import java.util.List;

public class BFS {
    public static void main(String[] args) {
        Vertex v1=new Vertex("v1",new LinkedList<>());
        Vertex v2=new Vertex("v2",new LinkedList<>());
        Vertex v3=new Vertex("v3",new LinkedList<>());
        Vertex v4=new Vertex("v4",new LinkedList<>());
        Vertex v5=new Vertex("v5",new LinkedList<>());
        Vertex v6=new Vertex("v6",new LinkedList<>());

        v1.edges.add(new Edge(v3,9));
        v1.edges.add(new Edge(v2,7));
        v1.edges.add(new Edge(v6,14));

        v2.edges.add(new Edge(v4,15));

        v3.edges.add(new Edge(v4,11));
        v3.edges.add(new Edge(v6,2));

        v4.edges.add(new Edge(v5,6));

        v6.edges.add(new Edge(v5,9));
        bfs(v1);
    }
    public static void bfs(Vertex vertex){
        LinkedList<Vertex> queue=new LinkedList<>();
        queue.offer(vertex);
        vertex.flag=true;
        while (!queue.isEmpty()){
            Vertex poll = queue.poll();
            System.out.println(poll.name);
            for (Edge edge : poll.edges) {
                if(!edge.vertex.flag){
                    edge.vertex.flag=true;
                    queue.offer(edge.vertex);
                }
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值