Graph Data Structure Intro (inc. adjacency list, adjacency matrix, incidence matrix)
Skills:
Algorithm Basics90%
Key Takeaways
Introduction to graph data structures, including adjacency list, adjacency matrix, and incidence matrix
Full Transcript
The graph data structure is not the same as a graph you may have learned about in math class. Graphs are collections of things and the relationships or connections between them. The data in a graph are called nodes or vertices. The connections between the nodes are called edges. One example of graphs is a social network where the nodes are you and other people and the edges are whether two people are friends with each other. There are two major types of graphs, directed and undirected. Undirected graphs are graphs without any direction on the edges between nodes. Directed graphs are graphs with a direction in its edges. An example of an undirected graph could be a social network. The nodes are people and the edges are friendships. An example of a directed graph could be the internet and web page links. The nodes are web pages and the directed edges are links to other pages, which might not necessarily point the other way. I'm going to show you three ways to represent a graph. The first way is called an adjacency list. This representation for a graph associates each vertex in the graph with the collection of its neighboring vertices or edges. In this image, A is connected to B, B is connected to A and C, and C is connected to B. This is how you could show a a relationship with text. And here is how you could show this adjacency list with JavaScript. This is an undirected graph because it does not show the direction of the edges. This can also be more simply represented as an array where the nodes just have numbers rather than string labels. Another way to represent a graph is to put it in an adjacency matrix. An adjacency matrix is a two-dimensional array where each nested array has the same number of elements as the outer array. So, it's basically a matrix of numbers where the numbers represent the edges. Zeros means there is no edge or relationship and ones mean there is a relationship. This table shows an adjacency matrix to represent the image. You can see that the labels for the nodes are on the top and left. Now, here's a JavaScript representation of the same thing. Unlike an adjacency list, each row of the matrix has to have the same number of elements as nodes in the graph. Here, we have a 3 by 3 matrix, which means we have three nodes in our graph. An adjacency matrix can be used to represent a directed graph. Here's a graph where the second node has an edge pointing toward the first node, and then the third node has an edge pointing to the first node. Notice how the numbers in the array change. There are only ones where a node is pointing toward another node, and since there only two points, there are only two nodes. The final way I will show to represent a graph is an incidence matrix. Like the adjacency matrix, an incidence matrix is a two-dimensional array. However, the rows and columns mean something else here. The adjacency matrix use both rows and columns to represent nodes. An incidence matrix uses rows rows to represent nodes and the columns to represent edges. This means that we can have an uneven number of rows and columns. Each column will represent a unique edge. Also, each edge connects two nodes. To show that there is edge between two nodes, you will put a one in the two rows of a particular column. As you can see in the diagram, edge one is connected to nodes A and B. Now, look at the column for edge one in the incidence matrix table. You will see a one in both the A row and the B row. This shows that edge one connects to nodes A and B. Here is a directed graph. For a directed graph, use -1 for an edge leaving a particular node and 1 for an edge entering a node. And here is a JavaScript implementation of the incidence matrix. Graphs can also have weights on their edges. So far, we have unweighted edges where just the presence and lack of edges binary, 0 or 1. You can have different weights depending on your application. A different weight is represented as a number greater than 1. Well, now you know about the different types of graphs and how to represent them in JavaScript. In my next video about graphs, I will cover graph traversal algorithms. Thanks for watching. My name is Beau Carnes. Don't forget to subscribe, and remember, use your code for good.
Original Description
Graphs are collections of things and the relationships or connections between them. The data in a graph are called nodes or vertices. The connections between the nodes are called edges. See how to represent an adjacency list, adjacency matrix, and incidence matrix in JavaScript.
🐦 Beau Carnes on Twitter: https://twitter.com/carnesbeau
⭐JavaScript Tutorials Playlists⭐
▶JavaScript Basics: https://www.youtube.com/playlist?list=PLWKjhJtqVAbk2qRZtWSzCIN38JC_NdhW5
▶Data Structures and Algorithms: https://www.youtube.com/playlist?list=PLWKjhJtqVAbkso-IbgiiP48n-O-JQA9PJ
▶Design Patterns: https://www.youtube.com/playlist?list=PLWKjhJtqVAbnZtkAI3BqcYxKnfWn_C704
▶ES6: https://www.youtube.com/playlist?list=PLWKjhJtqVAbljtmmeS0c-CEl2LdE-eR_F
▶Clean Code: https://www.youtube.com/playlist?list=PLWKjhJtqVAbkK24EaPurzMq0-kw5U9pJh
-
We're busy people who learn to code, then practice by building projects for nonprofits. Learn Full-stack JavaScript, build a portfolio, and get great references with our open source community.
Join our community at https://freecodecamp.com
Read great tech articles at https://medium.freecodecamp.com
❤️ Support for this channel comes from our friends at Scrimba – the coding platform that's reinvented interactive learning: https://scrimba.com/freecodecamp
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from freeCodeCamp.org · freeCodeCamp.org · 4 of 60
1
2
3
▶
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
React: Production Server Setup Part 2 - Live Coding with Jesse
freeCodeCamp.org
cookies vs localStorage vs sessionStorage - Beau teaches JavaScript
freeCodeCamp.org
Browser history tutorial - Beau teaches JavaScript
freeCodeCamp.org
Graph Data Structure Intro (inc. adjacency list, adjacency matrix, incidence matrix)
freeCodeCamp.org
React: Parameterized Routing with Next.js - Live Coding with Jesse
freeCodeCamp.org
React: Dealing with jQuery Issues - Live Coding with Jesse
freeCodeCamp.org
setInterval and setTimeout: timing events - Beau teaches JavaScript
freeCodeCamp.org
Browser and Device Testing - Live Coding with Jesse
freeCodeCamp.org
Last Minute Updates - Live Coding with Jesse
freeCodeCamp.org
Post Launch Updates - Live Coding with Jesse
freeCodeCamp.org
React: Setting Up Google Analytics - Live Coding with Jesse
freeCodeCamp.org
React: Masonry Layout - Live Coding with Jesse
freeCodeCamp.org
Load Balancing Digital Ocean Droplets - Live Coding with Jesse
freeCodeCamp.org
try, catch, finally, throw - error handling in JavaScript
freeCodeCamp.org
Load Balancing: SSL Passthrough Setup - Live Coding with Jesse
freeCodeCamp.org
Graphs: breadth-first search - Beau teaches JavaScript
freeCodeCamp.org
React: Masonry Layout Part 2 - Live Coding with Jesse
freeCodeCamp.org
React: WordPress API Live Search - Live Coding with Jesse
freeCodeCamp.org
Creating WordPress Custom Post Types - Live Coding With Jesse
freeCodeCamp.org
Dates - Beau teaches JavaScript
freeCodeCamp.org
Miscellaneous Front End Updates - Live Coding with Jesse
freeCodeCamp.org
Merging a Pull Request from GitHub - Live Coding with Jesse
freeCodeCamp.org
React + Prettier + Standard JS - Live Coding with Jesse
freeCodeCamp.org
React: Sortable Responsive Table - Live Coding with Jesse
freeCodeCamp.org
Geolocation Sorting by Distance - Live Coding with Jesse
freeCodeCamp.org
Tradeoff Matrix - Agile Software Development
freeCodeCamp.org
The Definition of Ready - Agile Software Development
freeCodeCamp.org
Getting first React job without experience - Ask Preethi
freeCodeCamp.org
React: Google Analytics Click Tracking - Live Coding with Jesse
freeCodeCamp.org
Submitting a PR to an Open Source Project - Live Coding with Jesse
freeCodeCamp.org
Should I go back to school to get CS degree? - Ask Preethi
freeCodeCamp.org
Hero Section CSS Changes - Live Coding with Jesse
freeCodeCamp.org
Working Agreement - Agile Software Development
freeCodeCamp.org
A day at Pennybox with Co-Founder Reji Eapen
freeCodeCamp.org
React: Sorting and Filtering Data - Live Coding with Jesse
freeCodeCamp.org
React: Sorting and Filtering Data Part 2 - Live Coding with Jesse
freeCodeCamp.org
React: Building a New UI - Live Coding with Jesse
freeCodeCamp.org
Definition of Done - Agile Software Development
freeCodeCamp.org
Getting started with jQuery (tutorial) - Beau teaches JavaScript
freeCodeCamp.org
Making a React Blog with WordPress Content - Live Coding with Jesse
freeCodeCamp.org
React, NextJS, CSS - Live Coding with Jesse
freeCodeCamp.org
jQuery events - Beau teaches JavaScript
freeCodeCamp.org
React/NextJS Routing and WordPress API Custom Types - Live Coding with Jesse
freeCodeCamp.org
React: Working with API Data - Live Coding with Jesse
freeCodeCamp.org
React: Refactoring Components - Live Streaming with Jesse
freeCodeCamp.org
jQuery effects - Beau teaches JavaScript
freeCodeCamp.org
More React Refactoring - Live Coding with Jesse
freeCodeCamp.org
animate in jQuery - Beau teaches JavaScript
freeCodeCamp.org
"Finishing" My React Site - Live Coding with Jesse
freeCodeCamp.org
Starting a New React Project (P2D1) - Live Coding with Jesse
freeCodeCamp.org
React Project 2 Day 2: Learning Material UI - Live Coding with Jesse
freeCodeCamp.org
The Agile Manifesto - Agile Software Development
freeCodeCamp.org
jQuery: get and set with http, text, val, and attr - Beau teaches JavaScript
freeCodeCamp.org
React Project 2 Day 3 - Live Coding with Jesse
freeCodeCamp.org
The INVEST approach to product backlog items
freeCodeCamp.org
React Project 2 Day 4 - Live Coding with Jesse
freeCodeCamp.org
Chickens and Pigs - Agile Software Development
freeCodeCamp.org
React Project 2 Day 5 - Live Coding with Jesse
freeCodeCamp.org
jQuery: add and remove DOM elements - Beau teaches JavaScript
freeCodeCamp.org
React Project 2 Day 6 - Live Coding with Jesse
freeCodeCamp.org
More on: Algorithm Basics
View skill →Related Reads
📰
📰
📰
📰
75 Days of Leetcode — Day 4: #238 — Product of Array Except Self
Medium · AI
I implemented the algorithm that broke the sorting barrier. Dijkstra still wins.
Medium · Programming
I implemented the algorithm that broke the sorting barrier. Dijkstra still wins.
Medium · Python
Practice Algorithms and DS the Structured Way
Medium · Programming
🎓
Tutor Explanation
DeepCamp AI