📕
Dan Fitz's Notes
  • README
  • Ai
    • Supervised Machine Learning
      • Introduction To Machine Learning
      • Regression With Multiple Input Variables
      • Classification
  • Csharp
    • C Sharp Advanced
      • Generics
      • Delegates
      • Lambda Expressions
      • Events
    • C Sharp Fundamentals
      • Intro To C
      • Primitive Types And Expressions
      • Non Primitive Types
      • Control Flow
      • Arrays And Lists
      • Working With Dates
      • Working With Text
      • Working With Files
      • Debugging Applications
    • C Sharp Intermediate
      • Classes
      • Association Between Classes
      • Inheritance
      • Polymorphism
      • Interfaces
  • Java
    • Inheritance Data Structures Java
      • Inheritance Polymorphism Using Overriding And Access Modifiers
      • Abstract Classes And Debugging
      • File I O And Exceptions
      • Collections Maps And Regular Expressions
    • Intro To Java
      • Introduction To Java Classes And Eclipse
      • Unit Testing Arrays And Array Lists
      • Static Variables Methods And Polymorphism Using Overloading
  • Javascript
    • Algorithms Data Structures
      • Big O Notation
      • Analyzing Performance Of Arrays And Objects
      • Problem Solving Approach
      • Problem Solving Patterns
      • Recursion
      • Searching Algorithms
      • Bubble Selection And Insertion Sort
      • Merge Sort
      • Quick Sort
      • Radix Sort
      • Data Structures Introduction
      • Singly Linked Lists
      • Doubly Linked Lists
      • Stacks And Queues
      • Binary Search Trees
      • Tree Traversal
      • Binary Heaps
    • Complete Nodejs
      • Understanding Node.js
      • REST AP Is And Mongoose
      • API Authentication And Security
      • Node.js Module System
      • File System And Command Line Args
      • Debugging Node.js
      • Asynchronous Node.js
      • Web Servers
      • Accessing API From Browser
      • Application Deployment
      • Mongo DB And Promises
    • Complete React Native
      • Working With Content
      • Building Lists
      • Navigating Users Between Screens
      • State Management
      • Handling Screen Layout
      • Setting Up An App
      • More On Navigation
      • Advanced Statement Management With Context
      • Building A Custom Express API
      • In App Authentication
    • Epic React
      • React Fundamentals
      • React Hooks
      • Advanced React Hooks
      • Advanced React Patterns
      • React Performance
    • Fireship Firestore
      • Firestore Queries And Data Modeling Course
      • Model Relational Data In Firestore No SQL
    • Functional Light Javascript
      • Intro
      • Function Purity
      • Argument Adapters
      • Point Free
      • Closure
      • Composition
      • Immutability
      • Recursion
      • List Operations
      • Transduction
      • Data Structure Operations
      • Async
    • Js Weird Parts
      • Execution Contexts And Lexical Environments
      • Types And Operators
      • Objects And Functions
      • Object Oriented Java Script And Prototypal Inheritance
      • Defining Objects
    • Mastering Chrome Dev Tools
      • Introduction
      • Editing
      • Debugging
      • Networking
      • Auditing
      • Node.js Profiling
      • Performance Monitoring
      • Image Performance
      • Memory
    • React Complete Guide
      • What Is React
      • React Basics
      • Rendering Lists And Conditionals
      • Styling React Components
      • Debugging React Apps
      • Component Deep Dive
      • Building A React App
      • Reaching Out To The Web
      • Routing
    • React Testing
      • Intro To Jest Enzyme And TDD
      • Basic Testing
      • Redux Testing
      • Redux Thunk Testing
    • Serverless Bootcamp
      • Introduction
      • Auction Service Setup
      • Auction Service CRUD Operations
      • Auction Service Processing Auctions
    • Testing Javascript
      • Fundamentals Of Testing
      • Static Analysis Testing
      • Mocking Fundamentals
      • Configuring Jest
      • Test React Components With Jest And React Testing Library
    • Typescript Developers Guide
      • Getting Started With Type Script
      • What Is A Type System
      • Type Annotations In Action
      • Annotations With Functions And Objects
      • Mastering Typed Arrays
      • Tuples In Type Script
      • The All Important Interface
      • Building Functionality With Classes
    • Web Performance With Webpack
      • Intro
      • Code Splitting
      • Module Methods Magic Comments
  • Other
    • Algo Expert
      • Defining Data Structures And Complexity Analysis
      • Memory
      • Big O Notation
      • Logarithm
      • Arrays
      • Linked Lists
      • Hash Tables
      • Stacks And Queues
      • Strings
      • Graphs
      • Trees
    • Aws Solutions Architect
      • AWS Fundamentals IAM EC 2
    • Fundamentals Math
      • Numbers And Negative Numbers
      • Factors And Multiples
      • Fractions
    • Mysql Bootcamp
      • Overview And Installation
      • Creating Databases And Tables
      • Inserting Data
      • CRUD Commands
      • The World Of String Functions
      • Refining Our Selections
      • The Magic Of Aggregate Functions
    • Random Notes
      • Understanding React Hooks
  • Python
    • Data Analysis Using Python
      • Loading Querying And Filtering Data Using The Csv Module
      • Loading Querying Joining And Filtering Data Using Pandas
      • Summarizing And Visualizing Data
    • Intro To Python
      • Course Introduction Intro To Programming And The Python Language Variables Conditionals Jupyter Notebook And IDLE
      • Intro To Lists Loops And Functions
      • More With Lists Strings Tuples Sets And Py Charm
      • Dictionaries And Files
Powered by GitBook
On this page
  • Important Properties of Graphs
  • Connectivity
  • Direction
  • Cycles
  • Representing a Graph in Code
  • Space Complexity of Graphs
  • Traversal operations
  • Depth-first search
  • Breadth-first search
  • Time complexity of traversal
  1. Other
  2. Algo Expert

Graphs

Graphs are a collection of nodes that may or may not be connected to one another.

The nodes are called vertices, and the connections are called edges.

Important Properties of Graphs

Connectivity

A graph is considered connected when, for any two possible vertices, there always exists some path connecting them. In other words, no vertex is unreachable. A real-world example of a connected graph would be a network of tight-knit friends (the friendships being the edges).

Note: By default, we call these connections weakly connected because the connections have at least one direction. A graph is only strongly connected when the paths that connect any two vertices are always bi-directional: you can get from vertex 1 to vertex 2 just as easily as you can get from vertex 2 to vertex 1.

On the other hand, if there are unreachable vertices, a graph is considered disconnected. A real-world example would be a group that includes one person that has no friends (so their vertex has no edges).

Direction

A graph is considered directed when its edges point from one vertex to another, i.e., its edges can be represented as arrows. A real-world example of a directed graph would be a network of airports and their flight paths. Pearson Airport can have a flight path to Heathrow Airport, and that direction matters.

Otherwise, a graph is considered undirected when direction doesn't make sense for the edges, often because the relationship is always bi-directional. A real-world example of an undirected graph would be a network of friends because friendship always goes both ways.

Cycles

As a decent heuristic, a graph is considered cyclic when you have 3 or more vertices with edges that form a circle, allowing you to traverse around the same vertices infinitely. A real-world example of a cyclic graph would be a Wikipedia article that links to other articles that eventually link back to the original article.

Otherwise, a graph is considered acyclic.

Pro tip: When traversing through cyclic graphs, be aware that it's possible to get locked into an infinite loop. To avoid this problem, you have techniques available like marking vertices as visited and skipping the visited ones.

Representing a Graph in Code

Graphs are represented in code via adjacency lists. Adjacency lists are defined by 2 features:

  • Store vertices/nodes with their values in a list or hash table

  • In each vertex/node, keep a list of the vertices that it's connected to, i.e., the edges leading to the adjacent vertices

Space Complexity of Graphs

Generally, when storing a graph, there are V vertices and E edges. So, we say the space complexity of a graph is O(V + E).

Traversal operations

Traversal is the most common operation you're likely to perform for graphs, so it's valuable to dedicate a section to it.

Depth-first search

The fundamental idea of depth-first search is that you prioritize depth when traversing through a vertex's children. This means going deep before wide: going as deep as possible for one vertex before moving onto the next vertex.

For example, suppose vertex 1 has children 2, 3, and 4. By prioritizing depth, you focus on vertex 2 first, traversing down a straight line until you hit the last vertex in that line. Only when you're done do you move onto the next child vertex 3.

Breadth-first search

In contrast, the fundamental idea of breadth-first search is that you prioritize width when traversing through a vertex's children. This means going wide before deep: going as wide as possible between child vertices before diving deeper into any one particular vertex.

For example, suppose vertex 1 has children 2, 3, and 4. By prioritizing breadth, you move through vertices 2, 3, and 4 in sequence. Only when you're done do you start diving deeper into any particular vertex.

Time complexity of traversal

Without going into it too much yet, traversal ends up being O(V + E) just like space complexity because you can imagine having to traverse through every single vertex and every single edge.

PreviousStringsNextTrees

Last updated 3 years ago