使用广度优先搜索算法检测无向图中的循环

问题描述

使用这个算法我得到了错误的答案,但我的测试用例是 给出正确答案。让我知道我哪里出错了。在这 算法我使用广度优先搜索(BFS)技术来找到 检测循环。

package com.example.bookstore;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class BookAdapter extends RecyclerView.Adapter<BookAdapter.Holderview> {

    private List<Book> BookHolderList;
    private Context context;
    private RecyclerViewClickListener listener;

    public BookAdapter(List<Book> bookList,Context context,RecyclerViewClickListener listener) {
        this.BookHolderList = bookList;
        this.context = context;
        this.listener = listener;
    }

    @Override
    public Holderview onCreateViewHolder(ViewGroup parent,int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.design_book,null);
        return new Holderview(view);
    }

    @Override
    public void onBindViewHolder(Holderview holder,final int position) {
        holder.txtName.setText(BookHolderList.get(position).getName());
        holder.txtPrice.setText(BookHolderList.get(position).getPrice());
        holder.imageView.setBackgroundResource(BookHolderList.get(position).getImage());


        //Animation code
        //holder.constraintLayout.setAnimation(AnimationUtils.loadAnimation(context,R.anim.fade_scale_animation));
    }

    @Override
    public int getItemCount() {
        return BookHolderList.size();
    }

    public interface RecyclerViewClickListener{
        void onClick(View v,int position);
    }
    public class Holderview extends RecyclerView.ViewHolder implements View.OnClickListener{

        TextView txtName,txtPrice;
        ImageView imageView;

        public Holderview(View itemView) {
            super(itemView);

            imageView = itemView.findViewById(R.id.imageView);
            txtName = itemView.findViewById(R.id.TvBookName);
            txtPrice = itemView.findViewById(R.id.TvBookPrice);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            listener.onClick(v,getAdapterPosition());
        }
    }
}

解决方法

假设

该方法假设输入的无向图是连通的。

请检查对于问题域的所有可能输入是否是有效假设。

可能的解决方案

  1. 现有方法适用于单个森林
  2. 向现有方法添加额外的 startNodevisited 参数
  3. 添加另一个包装器方法,该方法将为每个未访问的节点(森林)调用现有方法。

  boolean hasCycle(final int N,final List<List<Integer>> graph) {
    final boolean[] visited = boolean[N];

    for (int vertex = 0; vertex < N; vertex++) {
      if (!visited[vertex] && hasCycle(N,vertex,visited,graph)) {
        return true;
      }
    }

    return false;
  }

两种方法

  boolean hasCycle(final int N,final int start,final boolean[] visited,final List<List<Integer>> graph) {
    visited[start] = true;

    final int parent[] = new int[N];
    parent[start] = -1;

    final Queue<Integer> q = new LinkedList<>();
    q.add(start);

    while(!q.isEmpty()) {
      final int node = q.poll();

      // assumes for nodes without edges,an empty list will be returned
      for(int adj : graph.get(node)) {
        if (!visited[adj]) {
          q.add(adj);
          parent[adj] = node;
          visited[adj] = true;
        } else if (parent[node] != adj) {
          return true;
        }
      }
    }

    return false;
  }

  boolean hasCycle(final int N,graph)) {
        return true;
      }
    }

    return false;
  }

免责声明

这是未经测试和未编译的代码(输入 SO)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...