INTRODUCTION TO
GRAPHS

INTRODUCTION TO GRAPHS

PRESENTED BY BIANCA GANDOLFO

CONCEPT

Graphs are a set of vertices connected by edges

Each item in the graph contains:

1. Stored data -- aka node value

2. Stored references --aka relationships

​  aka  edges -- to zero or more other nodes

1

4

3

5

2

6

INTRODUCTION TO GRAPHS

PRESENTED BY BIANCA GANDOLFO

PRESENTED BY BIANCA GANDOLFO

VOCABULARY

Edges

  represent the connection between 2 vertices

  can be directed or undirected

Vertices

  nodes in the graph

Path

  a sequence of connected vertices

  a simple path has no repeated vertices

 

Cycles

  a path that is cyclical

  an acyclic graph has no cycles

INTRODUCTION TO GRAPHS

PRESENTED BY BIANCA GANDOLFO

PRESENTED BY BIANCA GANDOLFO

COMMON OPERATIONS

ADDING AN EDGE

DELETING AN EDGE

DETECTING AN EDGE

FINDING THE NEIGHBORS OF A VERTEX

FINDING A PATH BETWEEN TWO VERTICES

INTRODUCTION TO GRAPHS

PRESENTED BY BIANCA GANDOLFO

REPRESENTING
GRAPHS

INTRODUCTION TO GRAPHS

PRESENTED BY BIANCA GANDOLFO

ADJACENCY MATRIX

Undirected Graph

INTRODUCTION TO GRAPHS

PRESENTED BY BIANCA GANDOLFO

INTRODUCTION TO GRAPHS

PRESENTED BY BIANCA GANDOLFO

ADJACENCY MATRIX

Directed Graph

ADJACENCY MATRIX

Weighted

Directed Graph

INTRODUCTION TO GRAPHS

PRESENTED BY BIANCA GANDOLFO

PSEUDOCODE

addNode()

addEdge()
Constructor

INTRODUCTION TO GRAPHS

PRESENTED BY BIANCA GANDOLFO

ADJACENCY LIST

Directed Graph

INTRODUCTION TO GRAPHS

PRESENTED BY BIANCA GANDOLFO

PSEUDOCODE

addNode()

addEdge()
Constructor

CONSIDER:

How could we represent (un)directed edges?

How could we represent weighted edges?

INTRODUCTION TO GRAPHS

PRESENTED BY BIANCA GANDOLFO

EXERCISE TIME!

INTRODUCTION TO GRAPHS

PRESENTED BY BIANCA GANDOLFO

INTRODUCTION TO

 

DEPTH-FIRST SEARCH (DFS)

INTRODUCTION TO GRAPHS

PRESENTED BY BIANCA GANDOLFO

GRAPH TRAVERSING

Why?

Find paths, cycles, connectivity and more!

 

Concepts:

Explored (black)

Visited (gray)

Undiscovered (white)

INTRODUCTION TO GRAPHS

PRESENTED BY BIANCA GANDOLFO

PSEUDOCODE

//Code here

INTRODUCTION TO GRAPHS

PRESENTED BY BIANCA GANDOLFO

PROCEDURE

1. Mark v as discovered (grey).

2.  All unvisited (white) neighbors w of v:

     Visit vertex w.

3. Mark v as explored (black).

INTRODUCTION TO GRAPHS

PRESENTED BY BIANCA GANDOLFO

DEPTH-FIRST SEARCH (DFS)

O(n)
O(n)O(n)

Time

INTRODUCTION TO GRAPHS

PRESENTED BY BIANCA GANDOLFO

EXERCISE TIME!

INTRODUCTION TO GRAPHS

PRESENTED BY BIANCA GANDOLFO

INTRODUCTION TO

 

BREADTH-FIRST SEARCH (BFS)

INTRODUCTION TO GRAPHS

PRESENTED BY BIANCA GANDOLFO

CONCEPT: BFS

INTRODUCTION TO GRAPHS

PRESENTED BY BIANCA GANDOLFO

DIAGRAM: BFS

INTRODUCTION TO GRAPHS

PRESENTED BY BIANCA GANDOLFO

PSEUDOCODE

//Code here

INTRODUCTION TO GRAPHS

PRESENTED BY BIANCA GANDOLFO

PROCEDURE

1. Create a queue Q.

2. Mark v as discovered (grey) and enqueue v into Q.

3. While Q is not empty, perform the following steps:

1. Dequeue u from Q.

2. Mark u as discovered (grey).

3. Enqueue all unvisited (white) neighbors w of u.

4. Mark u as explored (black).

INTRODUCTION TO GRAPHS

PRESENTED BY BIANCA GANDOLFO

INTERFACE: BFS

1. BFS Function

      - Queue

     - Recursion

 

2. Tree Graph versus Data Structure

INTRODUCTION TO GRAPHS

PRESENTED BY BIANCA GANDOLFO

TIME COMPLEXITY

O(n)
O(n)O(n)

INTRODUCTION TO GRAPHS

PRESENTED BY BIANCA GANDOLFO

USE CASES: BFS

1. Shortest Path Finding

2. Web Crawlers

INTRODUCTION TO GRAPHS

PRESENTED BY BIANCA GANDOLFO

EXERCISE TIME!

INTRODUCTION TO GRAPHS

PRESENTED BY BIANCA GANDOLFO

RECAP

INTRODUCTION TO GRAPHS

PRESENTED BY BIANCA GANDOLFO

  • DFS and BFS can be applied to graphs and trees
  • information about how data is organized in the graph or tree can aid in determining whether to use DFS or BFS
  • BFS can be implemented using helper data structures
  • DFS can be implemented recursively and is the simpler of the two methods to implement

RECAP: CONCEPTS

INTRODUCTION TO GRAPHS

PRESENTED BY BIANCA GANDOLFO

EXERCISE TIME!

INTRODUCTION TO GRAPHS

PRESENTED BY BIANCA GANDOLFO

INTRODUCTION TO GRAPHS

PRESENTED BY BIANCA GANDOLFO

INTRODUCTION TO GRAPHS

Intro to Graphs & Paths

By Bianca Gandolfo

Intro to Graphs & Paths

  • 2,074