Professional Documents
Culture Documents
6.1. Intro. To Graphs
6.1. Intro. To Graphs
6.1. Intro. To Graphs
Graphs: Motivation
Ibrahim Albluwi
Seven Bridges of Königsberg
Seing.
1736 Königsberg in Prussia.
Motivation.
Show tourists the beauty of the
city in an efficient way!
Problem.
Starting at any place in the city,
can we cross the seven bridges
without crossing any bridge
twice?
Seing.
1736 Königsberg in Prussia.
Motivation.
Show tourists the beauty of the
city in an efficient way!
Problem.
Starting at any place in the city,
can we cross the seven bridges
without crossing any bridge
twice?
Result.
Visiting every edge only once in
an undirected connected graph
is possible if and only if exactly
0 or 2 nodes have odd degree.
Significance.
First known proof in graph
theory!
Morale.
We can think of the problem
abstractly if we represent it on
a graph.
Consequence.
Unrelated problems sharing the
same structure share the same
solution!
Example # 1
What is the min number of hops a packet needs How many levels of connections separate
to make between 2 given devices on the network? two given people on a social network?
What is the min number of transits a traveler What is the min number of edges separating
needs to make from a city to another? two nodes in the graph?
Example # 2
5 1
8 6
9 3
7
2
7
1
1
6
9 8
2 1 7 2 3
22 11 11 5 4
3 50 8 9 1
What is the max number of apples that can be What is the longest path between
collected from the top le corner to the boom two given nodes in the graph?
right corner, moving only right or down.
Agenda
• Topological Sort.
Undirected Graph.
• Edges are "unordered pairs": (u, v) = (v, u).
• A vertex v is adjacent to u if there is an edge (u, v).
• e degree of a node v is number of nodes adjacent to v.
degree = 1
degree = 4
Definitions
in-degree = 2
out-degree = 1
Definitions
Weighted Graphs.
Each edge has a weight (e.g. representing a distance between cities,
the number of citations between papers, the number of shared tweets between two
accounts, the cost of a transaction between two entities, etc.)
Definitions
Cycle. A path from u to w containing ≥ 2 edges, where u and w are the same vertex.
Simple Cycle. Has no repeated vertices except the starting and end vertices.
Definitions
self loop
B C
parallel edges
E F
A B C A B C A B C
D E H D E H D E H
Connected Graph.
An undirected graph is connected if every vertex is reachable from every vertex.
Connected Component.
A subgraph that is connected. A connected graph has one connected component.
Definitions
Sparse Graph. Has O(V ) edges Dense Graph. Has Θ(V 2) edges.
Note. is definition is loose. ere is no clear threshold between dense and
sparse graphs, but some graphs are clearly dense and some are clearly sparse.
Exercise
A B
Every dense undirected graph is cyclic.
E
Every undirected graph with ≥ | V | edges is cyclic. C D
counterexample
Every digraph with ≥ | V | edges is cyclic.
Exercise
Symmetric!
Graph Representation
Adjacency List. An array (or list) of arrays (or lists), where list 0 stores the vertices
adjacent to vertex 0, list 1 stores the vertices adjacent to vertex 1, etc.
3 4
2
If the graph is weighted we can store in the adjacency list pairs (vertex, weight) instead
of just integers. We can also store objects of type Edge (holding two vertices and a weight).
Exercise
Fill out the table below with the running time (use the tightest bound possible.)
Memory.
Adjacency Matrix: Θ(V 2)
Adjacency List: Θ(E + V ) = Θ(V 2) if the graph is dense.
= Θ(V ) if the graph is sparse
Overall Comparison.
e adjacency matrix is beer for dense graphs.
Memory: Both require Θ(V 2)
Time: Adjacency matrices are as good as adjacency lists or faster.