Java拓扑排序知识点(含面试大厂题和源码)

文章介绍了在技术面试中,大厂常考的图算法问题,包括深度优先搜索、广度优先搜索的Java实现以及检测图中环的DFS方法,测试了候选人的图算法理解和实践能力。
摘要由CSDN通过智能技术生成

在技术面试中,大厂可能会要求候选人实现或优化一些与图相关的算法,比如深度优先搜索(DFS)、广度优先搜索(BFS)和拓扑排序等。以下是三道与这些算法相关的面试题目,以及它们的Java源码示例。

1. 实现深度优先搜索(DFS)

题目:实现一个深度优先搜索算法,用于递归地遍历一个图的所有顶点。

源码

import java.util.ArrayList;
import java.util.List;

public class DFSGraph {
    private final int V; // 顶点的数量
    private final List<List<Integer>> adj; // 邻接表

    public DFSGraph(int V) {
        this.V = V;
        adj = new ArrayList<>(V);
        for (int i = 0; i < V; i++) {
            adj.add(new ArrayList<>());
        }
    }

    public void addEdge(int source, int dest) {
        adj.get(source).add(dest);
    }

    public void DFS(int v) {
        boolean[] visited = new boolean[V];
        DFSUtil(v, visited);
    }

    private void DFSUtil(int v, boolean[] visited) {
        visited[v] = true;
        System.out.print(v + " ");

        for (int i : adj.get(v)) {
            if (!visited[i]) {
                DFSUtil(i, visited);
            }
        }
    }

    public static void main(String[] args) {
        DFSGraph graph = new DFSGraph(4);
        graph.addEdge(0, 1);
        graph.addEdge(1, 2);
        graph.addEdge(2, 3);
        graph.addEdge(3, 1);

        graph.DFS(0);
    }
}

2. 实现广度优先搜索(BFS)

题目:实现一个广度优先搜索算法,用于遍历一个图,并找到最短路径。

源码

import java.util.*;

public class BFSGraph {
    private final int V; // 顶点的数量
    private final List<List<Integer>> adj; // 邻接表

    public BFSGraph(int V) {
        this.V = V;
        adj = new ArrayList<>(V);
        for (int i = 0; i < V; i++) {
            adj.add(new ArrayList<>());
        }
    }

    public void addEdge(int source, int dest) {
        adj.get(source).add(dest);
    }

    public void BFS(int s) {
        boolean[] visited = new boolean[V];
        Queue<Integer> queue = new LinkedList<>();

        visited[s] = true;
        queue.offer(s);

        while (!queue.isEmpty()) {
            int v = queue.poll();
            System.out.print(v + " ");

            for (Integer i : adj.get(v)) {
                if (!visited[i]) {
                    visited[i] = true;
                    queue.offer(i);
                }
            }
        }
    }

    public static void main(String[] args) {
        BFSGraph graph = new BFSGraph(4);
        graph.addEdge(0, 1);
        graph.addEdge(1, 2);
        graph.addEdge(2, 3);
        graph.addEdge(3, 1);

        graph.BFS(0);
    }
}

3. 检测图是否包含环

题目:给定一个图,使用DFS实现一个算法来检测图中是否存在环。

源码

import java.util.ArrayList;
import java.util.List;

public class GraphCycleDetection {
    private final int V; // 顶点的数量
    private final List<List<Integer>> adj; // 邻接表
    private boolean[] visited;
    private boolean[] recStack;
    private boolean isCycle;

    public GraphCycleDetection(int V) {
        this.V = V;
        adj = new ArrayList<>(V);
        for (int i = 0; i < V; i++) {
            adj.add(new ArrayList<>());
        }
    }

    public void addEdge(int source, int dest) {
        adj.get(source).add(dest);
    }

    public boolean isGraphCyclic() {
        visited = new boolean[V];
        recStack = new boolean[V];
        isCycle = false;

        for (int i = 0; i < V; i++) {
            if (isCycle) break;
            if (!visited[i]) {
                detectCycle(i);
            }
        }

        return isCycle;
    }

    private void detectCycle(int v) {
        if (isCycle) return;

        visited[v] = true;
        recStack[v] = true;

        for (Integer neighbour : adj.get(v)) {
            if (!visited[neighbour] && detectCycle(neighbour)) {
                isCycle = true;
                return;
            } else if (recStack[neighbour]) {
                isCycle = true;
                return;
            }
        }

        recStack[v] = false;
    }

    public static void main(String[] args) {
        GraphCycleDetection graph = new GraphCycleDetection(4);
        graph.addEdge(0, 1);
        graph.addEdge(1, 2);
        graph.addEdge(2, 3);
        graph.addEdge(3, 1); // 此边创建环

        System.out.println("图中是否存在环: " + graph.isGraphCyclic());
    }
}

以上三道题目分别考察了DFS、BFS的实现以及如何使用DFS来检测图中的环。这些题目都是大厂面试中常见的题型,旨在考察候选人对图算法的理解和实现能力。在技术面试中,大厂可能会要求候选人实现或优化一些与图相关的算法,比如深度优先搜索(DFS)、广度优先搜索(BFS)和拓扑排序等。以下是三道与这些算法相关的面试题目,以及它们的Java源码示例。

1. 实现深度优先搜索(DFS)

题目:实现一个深度优先搜索算法,用于递归地遍历一个图的所有顶点。

源码

import java.util.ArrayList;
import java.util.List;

public class DFSGraph {
    private final int V; // 顶点的数量
    private final List<List<Integer>> adj; // 邻接表

    public DFSGraph(int V) {
        this.V = V;
        adj = new ArrayList<>(V);
        for (int i = 0; i < V; i++) {
            adj.add(new ArrayList<>());
        }
    }

    public void addEdge(int source, int dest) {
        adj.get(source).add(dest);
    }

    public void DFS(int v) {
        boolean[] visited = new boolean[V];
        DFSUtil(v, visited);
    }

    private void DFSUtil(int v, boolean[] visited) {
        visited[v] = true;
        System.out.print(v + " ");

        for (int i : adj.get(v)) {
            if (!visited[i]) {
                DFSUtil(i, visited);
            }
        }
    }

    public static void main(String[] args) {
        DFSGraph graph = new DFSGraph(4);
        graph.addEdge(0, 1);
        graph.addEdge(1, 2);
        graph.addEdge(2, 3);
        graph.addEdge(3, 1);

        graph.DFS(0);
    }
}

2. 实现广度优先搜索(BFS)

题目:实现一个广度优先搜索算法,用于遍历一个图,并找到最短路径。

源码

import java.util.*;

public class BFSGraph {
    private final int V; // 顶点的数量
    private final List<List<Integer>> adj; // 邻接表

    public BFSGraph(int V) {
        this.V = V;
        adj = new ArrayList<>(V);
        for (int i = 0; i < V; i++) {
            adj.add(new ArrayList<>());
        }
    }

    public void addEdge(int source, int dest) {
        adj.get(source).add(dest);
    }

    public void BFS(int s) {
        boolean[] visited = new boolean[V];
        Queue<Integer> queue = new LinkedList<>();

        visited[s] = true;
        queue.offer(s);

        while (!queue.isEmpty()) {
            int v = queue.poll();
            System.out.print(v + " ");

            for (Integer i : adj.get(v)) {
                if (!visited[i]) {
                    visited[i] = true;
                    queue.offer(i);
                }
            }
        }
    }

    public static void main(String[] args) {
        BFSGraph graph = new BFSGraph(4);
        graph.addEdge(0, 1);
        graph.addEdge(1, 2);
        graph.addEdge(2, 3);
        graph.addEdge(3, 1);

        graph.BFS(0);
    }
}

3. 检测图是否包含环

题目:给定一个图,使用DFS实现一个算法来检测图中是否存在环。

源码

import java.util.ArrayList;
import java.util.List;

public class GraphCycleDetection {
    private final int V; // 顶点的数量
    private final List<List<Integer>> adj; // 邻接表
    private boolean[] visited;
    private boolean[] recStack;
    private boolean isCycle;

    public GraphCycleDetection(int V) {
        this.V = V;
        adj = new ArrayList<>(V);
        for (int i = 0; i < V; i++) {
            adj.add(new ArrayList<>());
        }
    }

    public void addEdge(int source, int dest) {
        adj.get(source).add(dest);
    }

    public boolean isGraphCyclic() {
        visited = new boolean[V];
        recStack = new boolean[V];
        isCycle = false;

        for (int i = 0; i < V; i++) {
            if (isCycle) break;
            if (!visited[i]) {
                detectCycle(i);
            }
        }

        return isCycle;
    }

    private void detectCycle(int v) {
        if (isCycle) return;

        visited[v] = true;
        recStack[v] = true;

        for (Integer neighbour : adj.get(v)) {
            if (!visited[neighbour] && detectCycle(neighbour)) {
                isCycle = true;
                return;
            } else if (recStack[neighbour]) {
                isCycle = true;
                return;
            }
        }

        recStack[v] = false;
    }

    public static void main(String[] args) {
        GraphCycleDetection graph = new GraphCycleDetection(4);
        graph.addEdge(0, 1);
        graph.addEdge(1, 2);
        graph.addEdge(2, 3);
        graph.addEdge(3, 1); // 此边创建环

        System.out.println("图中是否存在环: " + graph.isGraphCyclic());
    }
}

以上三道题目分别考察了DFS、BFS的实现以及如何使用DFS来检测图中的环。这些题目都是大厂面试中常见的题型,旨在考察候选人对图算法的理解和实现能力。

  • 26
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值