Day 1 of Mastering Graphs

Day 1 of Mastering Graphs

In partnership with

TLDR

  1. Welcome!

  2. Intelligent automation from ELEKS

  3. Recommendations I live by!

  4. Self-Gaslight of the week - Master your behavioral interview

  5. Hacks of the week

  6. JOBS & SCHOLARSHIPS

Hello and welcome back brave human being!

I am about to turn 28, it sounds weird.

I never thought I would make it this far.

My early 20’s suuuuuucked.

  • Had a toxic boyfriend

  • My parents divorced

  • Felt lost

I struggled to regulate my emotions and maybe drank a little bit too much.

All this is to say that better things are yet to come.

As I near my 30s, I've never felt more content, more successful, or more at peace with myself.

But you must work on yourself. Tackle the hard things first, explore every possibility, and never shy away from growth.

By opening this newsletter, you're already taking those steps—so welcome to the journey!

LeetCode problem

Given a bi-directional graph with n vertices and edges represented as a 2D array, determine if a valid path exists between a specified source and destination vertex. Return true if a path exists, otherwise return false.

Before we dive in,

Breathe. I used to hyperventilate every time I saw a graph problem.

By the time you finish reading this email, you’ll be able to do a graph problem in your sleep.

The first thing for any graph problem is to build an adjacency matrix.

An adjacency matrix is just another way to show the relationships between nodes.

Each row and column corresponds to a node, and the matrix's values show which nodes are connected.

Since the graph is bidirectional, we reflect the relationship by pushing each connected node to both corresponding rows in the adjacency matrix.

Now let’s build it:

for(auto x: edges){
   adj[ x[0] ].push_back( x[1] );
   adj[ x[1] ].push_back( x[0] );
  }

Once we’ve built our adjacency matrix, the magic happens. 🪄 

We will recursively go through the matrix from source to destination and find whether or not there is a path.

To ensure we only visit each node once, we are going to keep a boolean vector, where we keep track of a node that has been visited or not.

We’ll start by iterating through the nodes connected to our source.

For each neighbor, we’ll check if it has already been visited. If it hasn’t, we’ll dive deeper, exploring the neighbors of that neighbor.

Once we’ve gone through the whole matrix, we can confirm visited[destination] is true, it means a path exists!

Complete code:

#include <vector>

class Solution {
public:


void DFS(vector<vector<int>>& adj, int source, vector<bool>& visited){
        visited[source] = true; 
        for(int x=0; x<adj[source].size(); x++){
            if(!visited[adj[source][x]]){
                DFS(adj,adj[source][x],  visited);
            }
        }
    }
    
bool validPath(int n, vector<vector<int>>& edges, int source, int destination) {
        vector<vector<int>> adj(n); 
        vector<bool> visited(n, false);
        for(auto x: edges){
           adj[x[0]].push_back(x[1]);
           adj[x[1]].push_back(x[0]);
        }

        DFS(adj,source, visited);

        return visited[destination];
    }
};

What would be the O Notation?

Where V represents Vertices and E represents the relationships

Login or Subscribe to participate in polls.

Power your competitive advantage with intelligent automation from ELEKS

ELEKS' intelligent automation service transforms your business operations through data-driven solutions. We automate complex tasks, streamlining processes to increase productivity and reduce operational costs. Our tailored solutions adapt to your changing needs and help you unlock new growth opportunities by freeing your team to focus on high-value tasks.

The result? Enhanced customer satisfaction, improved client retention, and a stronger market position.

Recommendations

Level up your inbox by subscribing to what I am subscribed to!

Self-gaslight of the week

I truly believe you can gaslight yourself to success.

Hacks of the week

  1. H + STAR + G method to behavioral interviews

  2. Never reveal you are unemployed, it gives desperation.

Cheers to you hacking your week,

Denisse

Reply

or to participate.