Graph Build Order

AlgorithmAlgorithmBeginner
Practice Now

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

Introduction

In software development, it is common to have projects that depend on other projects. In order to build these projects, we need to determine the order in which they should be built. This is known as the build order problem.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) algorithm(("`Algorithm`")) -.-> algorithm/BasicAlgorithmsGroup(["`Basic Algorithms`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) 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(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") algorithm/BasicAlgorithmsGroup -.-> algorithm/graphs_trees("`Graphs Trees`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/strings("`Strings`") 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/PythonStandardLibraryGroup -.-> python/data_collections("`Data Collections`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-268826{{"`Graph Build Order`"}} algorithm/graphs_trees -.-> lab-268826{{"`Graph Build Order`"}} python/with_statement -.-> lab-268826{{"`Graph Build Order`"}} python/variables_data_types -.-> lab-268826{{"`Graph Build Order`"}} python/strings -.-> lab-268826{{"`Graph Build Order`"}} python/type_conversion -.-> lab-268826{{"`Graph Build Order`"}} python/conditional_statements -.-> lab-268826{{"`Graph Build Order`"}} python/for_loops -.-> lab-268826{{"`Graph Build Order`"}} python/while_loops -.-> lab-268826{{"`Graph Build Order`"}} python/lists -.-> lab-268826{{"`Graph Build Order`"}} python/tuples -.-> lab-268826{{"`Graph Build Order`"}} python/function_definition -.-> lab-268826{{"`Graph Build Order`"}} python/default_arguments -.-> lab-268826{{"`Graph Build Order`"}} python/importing_modules -.-> lab-268826{{"`Graph Build Order`"}} python/using_packages -.-> lab-268826{{"`Graph Build Order`"}} python/standard_libraries -.-> lab-268826{{"`Graph Build Order`"}} python/classes_objects -.-> lab-268826{{"`Graph Build Order`"}} python/constructor -.-> lab-268826{{"`Graph Build Order`"}} python/polymorphism -.-> lab-268826{{"`Graph Build Order`"}} python/encapsulation -.-> lab-268826{{"`Graph Build Order`"}} python/raising_exceptions -.-> lab-268826{{"`Graph Build Order`"}} python/data_collections -.-> lab-268826{{"`Graph Build Order`"}} python/build_in_functions -.-> lab-268826{{"`Graph Build Order`"}} end

Graph Build Order

Problem

Given a list of projects and their dependencies, we need to find a valid build order. A build order is a list of projects in which each project appears before any project that depends on it.

Requirements

To solve this problem, we need to consider the following requirements:

  • The input may contain a cyclic graph.
  • We can assume that we already have Graph and Node classes.
  • We can assume that the graph is connected.
  • We can assume that the inputs are valid.
  • We can assume that the problem fits memory.

Example

Suppose we have the following projects and dependencies:

  • projects: a, b, c, d, e, f, g
  • dependencies: (d, g), (f, c), (f, b), (f, a), (c, a), (b, a), (a, e), (b, e)

The output should be: d, f, c, b, g, a, e

Note that the edge direction is down, meaning that a project depends on the projects below it.

    f     d
   /|\    |
  c | b   g
   \|/|
    a |
    |/
    e

If the input contains a cyclic graph, the output should be None.

Summary

In summary, the build order problem involves finding a valid order in which to build a list of projects and their dependencies. We can solve this problem by considering the requirements and using a Graph and Node class to represent the input.

Other Algorithm Tutorials you may like