INTRODUCTION TO
GRAPHS
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
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
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
PRESENTED BY BIANCA GANDOLFO
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
COMMON OPERATIONS
PRESENTED BY BIANCA GANDOLFO
ADDING AN EDGE
DELETING AN EDGE
DETECTING AN EDGE
FINDING THE NEIGHBORS OF A VERTEX
FINDING A PATH BETWEEN TWO VERTICES
REPRESENTING
GRAPHS
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
ADJACENCY MATRIX
Undirected Graph
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
ADJACENCY MATRIX
Directed Graph
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
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
DEPTH-FIRST SEARCH (DFS)
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
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)
Time
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
EXERCISE TIME!
INTRODUCTION TO
BREADTH-FIRST SEARCH (BFS)
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
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
INTERFACE: BFS
1. BFS Function
- Queue
- Recursion
2. Tree Graph versus Data Structure
INTRODUCTION TO GRAPHS
PRESENTED BY BIANCA GANDOLFO
TIME COMPLEXITY
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!
Copy of Graphs and Paths
By Julie Christie
Copy of Graphs and Paths
- 1,318