What is Depth First Order and Breadth First Order in Scipy explain with example

This recipe explains what is Depth First Order and Breadth First Order in Scipy with example

Recipe Objective - What is Depth First Order and Breadth First Order explain with example?

Depth First Search:

The Depth First Search is the method to find out a depth first traversal from a specific node. For that scipy provide "scipy.sparse.csgraph.depth_first_order()" method.

Depth First Search takes two arguments:

1. The graph

2. Starting element to traverse the graph from

import numpy as np
from scipy.sparse.csgraph import depth_first_order
from scipy.sparse import csr_matrix

arr = np.array([
[0, 0, 1, 0],
[0, 2, 1, 0],
[0, 1, 1, 1],
[1, 1, 0, 0]
])

new_arr = csr_matrix(arr)

print(depth_first_order(new_arr, 1))

Breadth First Order:

The Breadth First Order is the method to find out a breadth first traversal from a specific node. For that scipy provide "scipy.sparse.csgraph.breadth_first_order()" method.

Breadth First Order takes two arguments:

1. The graph

2. Starting element to traverse the graph from

import numpy as np
from scipy.sparse.csgraph import breadth_first_order
from scipy.sparse import csr_matrix

arr = np.array([
[1, 0, 1, 0],
[0, 1, 2, 1],
[1, 2, 0, 1],
[1, 0, 1, 0]
])

new_arr = csr_matrix(arr)

print(breadth_first_order(new_arr, 1))

What Users are saying..

profile image

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

OpenCV Project for Beginners to Learn Computer Vision Basics
In this OpenCV project, you will learn computer vision basics and the fundamentals of OpenCV library using Python.

Text Classification with Transformers-RoBERTa and XLNet Model
In this machine learning project, you will learn how to load, fine tune and evaluate various transformer models for text classification tasks.

ML Model Deployment on AWS for Customer Churn Prediction
MLOps Project-Deploy Machine Learning Model to Production Python on AWS for Customer Churn Prediction

Recommender System Machine Learning Project for Beginners-2
Recommender System Machine Learning Project for Beginners Part 2- Learn how to build a recommender system for market basket analysis using association rule mining.

Build a Speech-Text Transcriptor with Nvidia Quartznet Model
In this Deep Learning Project, you will leverage transfer learning from Nvidia QuartzNet pre-trained models to develop a speech-to-text transcriptor.

Recommender System Machine Learning Project for Beginners-3
Content Based Recommender System Project - Building a Content-Based Product Recommender App with Streamlit

Time Series Analysis with Facebook Prophet Python and Cesium
Time Series Analysis Project - Use the Facebook Prophet and Cesium Open Source Library for Time Series Forecasting in Python

Azure Deep Learning-Deploy RNN CNN models for TimeSeries
In this Azure MLOps Project, you will learn to perform docker-based deployment of RNN and CNN Models for Time Series Forecasting on Azure Cloud.

Build Regression Models in Python for House Price Prediction
In this Machine Learning Regression project, you will build and evaluate various regression models in Python for house price prediction.

Build OCR from Scratch Python using YOLO and Tesseract
In this deep learning project, you will learn how to build your custom OCR (optical character recognition) from scratch by using Google Tesseract and YOLO to read the text from any images.