Graph Path Exists

AlgorithmAlgorithmBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In computer science, a graph is a collection of nodes that are connected to each other through edges. Graphs are widely used to model real-world scenarios such as social networks, transportation systems, and computer networks. One of the fundamental problems in graph theory is to determine whether there is a path between two nodes in a graph. In this challenge, we will write a Python function to solve this problem.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) algorithm(("`Algorithm`")) -.-> algorithm/BasicAlgorithmsGroup(["`Basic Algorithms`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python(("`Python`")) -.-> python/ErrorandExceptionHandlingGroup(["`Error and Exception Handling`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") algorithm/BasicAlgorithmsGroup -.-> algorithm/graphs_trees("`Graphs Trees`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/strings("`Strings`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/BasicConceptsGroup -.-> python/type_conversion("`Type Conversion`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/ControlFlowGroup -.-> python/while_loops("`While Loops`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/default_arguments("`Default Arguments`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/constructor("`Constructor`") python/ObjectOrientedProgrammingGroup -.-> python/polymorphism("`Polymorphism`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/ErrorandExceptionHandlingGroup -.-> python/raising_exceptions("`Raising Exceptions`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-268828{{"`Graph Path Exists`"}} algorithm/graphs_trees -.-> lab-268828{{"`Graph Path Exists`"}} python/variables_data_types -.-> lab-268828{{"`Graph Path Exists`"}} python/strings -.-> lab-268828{{"`Graph Path Exists`"}} python/booleans -.-> lab-268828{{"`Graph Path Exists`"}} python/type_conversion -.-> lab-268828{{"`Graph Path Exists`"}} python/conditional_statements -.-> lab-268828{{"`Graph Path Exists`"}} python/for_loops -.-> lab-268828{{"`Graph Path Exists`"}} python/while_loops -.-> lab-268828{{"`Graph Path Exists`"}} python/lists -.-> lab-268828{{"`Graph Path Exists`"}} python/tuples -.-> lab-268828{{"`Graph Path Exists`"}} python/function_definition -.-> lab-268828{{"`Graph Path Exists`"}} python/default_arguments -.-> lab-268828{{"`Graph Path Exists`"}} python/importing_modules -.-> lab-268828{{"`Graph Path Exists`"}} python/using_packages -.-> lab-268828{{"`Graph Path Exists`"}} python/standard_libraries -.-> lab-268828{{"`Graph Path Exists`"}} python/classes_objects -.-> lab-268828{{"`Graph Path Exists`"}} python/constructor -.-> lab-268828{{"`Graph Path Exists`"}} python/polymorphism -.-> lab-268828{{"`Graph Path Exists`"}} python/encapsulation -.-> lab-268828{{"`Graph Path Exists`"}} python/raising_exceptions -.-> lab-268828{{"`Graph Path Exists`"}} python/build_in_functions -.-> lab-268828{{"`Graph Path Exists`"}} end

Graph Path Exists

Problem

Given a directed graph and two nodes, write a Python function to determine whether there is a path between the two nodes.

Requirements

To solve this problem, we need to make the following assumptions:

  • The graph is directed.
  • We already have Graph and Node classes.
  • The graph is connected.
  • The inputs are valid.
  • The solution fits memory.

Example Usage

Suppose we have the following graph:

graph.add_edge(0, 1, 5)
graph.add_edge(0, 4, 3)
graph.add_edge(0, 5, 2)
graph.add_edge(1, 3, 5)
graph.add_edge(1, 4, 4)
graph.add_edge(2, 1, 6)
graph.add_edge(3, 2, 7)
graph.add_edge(3, 4, 8)

We can use the following function to determine whether there is a path between two nodes:

search_path(start=0, end=2) -> True
search_path(start=0, end=0) -> True
search_path(start=4, end=5) -> False

The first two function calls return True because there is a path between the start and end nodes. The last function call returns False because there is no path between the start and end nodes.

Summary

In this challenge, we wrote a Python function to determine whether there is a path between two nodes in a directed graph. We made several assumptions about the graph and the inputs to simplify the problem. The function can be used to solve a wide range of real-world problems that involve graph traversal.

Other Algorithm Tutorials you may like