6.1. Intro. To Graphs

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

CS11313 - Fall 2023

Design & Analysis


of Algorithms

Graphs: Motivation
Ibrahim Albluwi
Seven Bridges of Königsberg

Seing.
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?

Enters Euler ...


Seven Bridges of Königsberg

Seing.
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?

How Euler Saw It.


Can we traverse every edge in
the following graph only once?

Challenge. ere was no such a


thing called graph theory!
Seven Bridges of Königsberg

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?

Underlying Graph Problem

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

What is the shortest path between PSUT and Alquds?

Underlying Graph Problem

5 1

8 6
9 3

7
2
7

1
1

6
9 8

What is the shortest path between


What sequence of exchanges from a currency to two given nodes in the graph?
another leads to the minimum loss (or to profit) ?
Example # 3

Given the dependency graph


for the tasks of a project, find
the critical path
(the longest possible schedule
of tasks)

Underlying Graph Problem


1 10 3 1 1

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 boom two given nodes in the graph?
right corner, moving only right or down.
Agenda

• Graph definitions and properties.

• Graph representation (data structures).

• Graph traversal (breadth-first and depth-first traversals).

• Cycle detection in graphs.

• Topological Sort.

• Shortest paths (Dijkstra's algorithm).

• Minimum spanning trees (Prim's algorithm).


CS11313 - Fall 2023

Design & Analysis


of Algorithms

Graphs: Definitions & Properties


Ibrahim Albluwi
Definitions

Graph. A set of vertices (or nodes) V and edges (or links) E.


• V is a non-empty set {0, 1, . . . , | V | − 1}.
• E is a (possibly empty) set of pairs of vertices (u, v).

Note. For simplicity, we will oen use


V to mean | V | and E to mean | E |
Definitions

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

Directed Graph (Digraph).


• Edges are "ordered pairs": (u, v) ≠ (v, u).
• Edge (u, v) represents a one-way directed link from u to v.
• A vertex v is adjacent to u if there is an edge from u to v (i.e. (u, v))
• e in-degree of a node is the number of incoming edges.
• e out-degree of a node is the number of outgoing edges.

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

Path. A path from u to w is a set of edges connecting u and w.


• w is said to be reachable from u if there is a path from u to w.
• A path is said to be simple, if there are no repeated edges.
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. An edge (u, u). Note. In this course, graphs, cycles


Simple Graph. and paths are assumed to be simple,
Has no parallel edges and no self-loops. unless otherwise specified.

self loop

B C

parallel edges

E F

not a simple Graph


Definitions

Cyclic Graph. Has cycles.


Acyclic Graph. Does not have cycles.

Tree. Undirected Acyclic Graph.


Forest. A graph with multiple components, where every component is a tree.
DAG. Directed Acyclic Graph.

A B C A B C A B C

D E H D E H D E H

A DAG A Tree A Forest


Definitions

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

Strong Connectivity. digraph: directed graph


A digraph is strongly connected if every vertex is reachable from every vertex.
Weak Connectivity.
A digraph is weakly connected if the corresponding undirected graph is connected.
Definitions

Complete Graph. Every vertex is adjacent to every vertex.

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

Describe each of the graphs below using the correct keywords:

A. Facebook (vertices = people, edges = friendship relations)


Undirected Dense Acyclic
Directed Sparse Cyclic

B. Twier (vertices = people, edges = follow relations)


Undirected Dense Acyclic
Directed Sparse Cyclic

C. Major Airports (vertices = airports, edges = flights)


Undirected Dense Acyclic
Directed Sparse Cyclic

D. University Plan (vertices = courses, edges = prerequisite relations)


Undirected Dense Acyclic
Directed Sparse Cyclic
Exercise

How many edges are there in a


complete undirected graph?

In the example graph to the right, V = 5 and


E = 4(A) + 3(B) + 2(F) + 1(D) + 0(D)
V−1
V(V − 1)

E= v=
v=0
2

How many edges are there in a


complete directed graph?

Every vertex has an outgoing edge to the V − 1 other vertices, so E = V(V − 1)


Exercise

Mark each of the following as True or False:

T e maximum possible number of connected components in a graph is | V | .

If | E | < | V | − 1 the graph is guaranteed to be disconnected.

If | E | ≥ | V | − 1 the graph is guaranteed to be connected.

Every dense undirected graph is connected. This happens when there


are no edges in the graph
Every dense undirected graph is cyclic. (every component has 1 vertex)

Every undirected graph with ≥ | V | edges is cyclic.

Every digraph with ≥ | V | edges is cyclic.


Exercise

Mark each of the following as True or False:

T e maximum possible number of connected components in a graph is | V | .

T If | E | < | V | − 1 the graph is guaranteed to be disconnected.

If | E | ≥ | V | − 1 the graph is guaranteed to be connected.

Every dense undirected graph is connected. There won't be enough


edges to link every node
Every dense undirected graph is cyclic.

Every undirected graph with ≥ | V | edges is cyclic.

Every digraph with ≥ | V | edges is cyclic.


Exercise

Mark each of the following as True or False:

T e maximum possible number of connected components in a graph is | V | .

T If | E | < | V | − 1 the graph is guaranteed to be disconnected.

F If | E | ≥ | V | − 1 the graph is guaranteed to be connected.

Every dense undirected graph is connected.

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

Mark each of the following as True or False:

T e maximum possible number of connected components in a graph is | V | .

T If | E | < | V | − 1 the graph is guaranteed to be disconnected.

F If | E | ≥ | V | − 1 the graph is guaranteed to be connected.

F Every dense undirected graph is connected. can you come up


with a counterexample?

Every dense undirected graph is cyclic.

Every undirected graph with ≥ | V | edges is cyclic.

Every digraph with ≥ | V | edges is cyclic.


Exercise

Mark each of the following as True or False:

T e maximum possible number of connected components in a graph is | V | .

T If | E | < | V | − 1 the graph is guaranteed to be disconnected.

F If | E | ≥ | V | − 1 the graph is guaranteed to be connected.

F Every dense undirected graph is connected. A B

T Every dense undirected graph is cyclic.


D E

T Every undirected graph with ≥ | V | edges is cyclic.


F
counterexample
F Every digraph with ≥ | V | edges is cyclic.
Graph Representation

Adjacency Matrix. Matrix[i][j]=1 if there is an edge (i, j), and 0 otherwise.

How can we represent


weighted graphs?

Insert the weight of


the edge in the cell
instead of 0/1
1
Use a value that is
1 guaranteed not to be
an edge weight
1
(e.g. INFINITY)

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.)

Operation Adjacency Matrix Adjacency List

isAdjacnet(u, v) O(1) O(deg(u)) (worst case = Θ(V ))

neighbors(u) Θ(V ) Θ(deg(u)) (worst case = Θ(V ))

O(1) (if parallel edges are allowed)


addEdge(u, v) O(1)
O(deg(u)) (if they are not allowed)

O(deg(u) + deg(v)) (undirected graphs)

removeEdge(u, v) O(1) O(deg(u)) (directed graphs)

worst case = Θ(V )


Graph Representation

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 beer for dense graphs.
Memory: Both require Θ(V 2)
Time: Adjacency matrices are as good as adjacency lists or faster.

e adjacency list is beer for sparse graphs.


Memory: Θ(V ) for lists and Θ(V 2) for matrices.
Time: Adjacency lists are almost as fast as adjacency matrices because
deg(u) is a small number (or even much faster, as in neighbors(u))

Note. Sparse Graph: deg(u) = O(1) on average


Dense Graph: deg(u) = Θ(V ) on average

You might also like