Graph Data Structure Fundamentals

AlgorithmAlgorithmBeginner
Practice Now

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

Introduction

A graph is a non-linear data structure consisting of nodes and edges. It is used to represent connections or relationships between objects. Graphs are widely used in computer science, engineering, and social sciences.


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/type_conversion("`Type Conversion`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") 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/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-268831{{"`Graph Data Structure Fundamentals`"}} algorithm/graphs_trees -.-> lab-268831{{"`Graph Data Structure Fundamentals`"}} python/variables_data_types -.-> lab-268831{{"`Graph Data Structure Fundamentals`"}} python/strings -.-> lab-268831{{"`Graph Data Structure Fundamentals`"}} python/type_conversion -.-> lab-268831{{"`Graph Data Structure Fundamentals`"}} python/conditional_statements -.-> lab-268831{{"`Graph Data Structure Fundamentals`"}} python/lists -.-> lab-268831{{"`Graph Data Structure Fundamentals`"}} python/tuples -.-> lab-268831{{"`Graph Data Structure Fundamentals`"}} python/function_definition -.-> lab-268831{{"`Graph Data Structure Fundamentals`"}} python/default_arguments -.-> lab-268831{{"`Graph Data Structure Fundamentals`"}} python/importing_modules -.-> lab-268831{{"`Graph Data Structure Fundamentals`"}} python/using_packages -.-> lab-268831{{"`Graph Data Structure Fundamentals`"}} python/classes_objects -.-> lab-268831{{"`Graph Data Structure Fundamentals`"}} python/constructor -.-> lab-268831{{"`Graph Data Structure Fundamentals`"}} python/polymorphism -.-> lab-268831{{"`Graph Data Structure Fundamentals`"}} python/encapsulation -.-> lab-268831{{"`Graph Data Structure Fundamentals`"}} python/raising_exceptions -.-> lab-268831{{"`Graph Data Structure Fundamentals`"}} python/build_in_functions -.-> lab-268831{{"`Graph Data Structure Fundamentals`"}} end

Graph

Problem

Implement a graph that satisfies the following requirements:

Requirements

  • The graph can be directed or undirected.
  • The edges can have weights.
  • The graph can have cycles.
  • If we try to add a node that already exists, we just do nothing.
  • If we try to delete a node that doesn't exist, we just do nothing.
  • We can assume this is a connected graph.
  • We can assume the inputs are valid.
  • We can assume this fits memory.

Example Usage

Input:

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

Result:

  • The nodes 0, 1, 2, 3, 4, and 5 are connected with specified weights.

Note:

  • The Graph class will be used as a building block for more complex graph challenges.

Summary

In this challenge, we implemented a graph that can be directed or undirected, have weighted edges, and cycles. We also ensured that adding or deleting nodes that already exist or do not exist respectively, does not affect the graph. The Graph class can be used as a building block for more complex graph challenges.

Other Algorithm Tutorials you may like