Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
42 views

Graphs: Algorithmic Thinking Luay Nakhleh

The document discusses graphs and their basic definitions and representations. It defines graphs as consisting of nodes and edges, and introduces key graph terminology like degrees, paths, connectivity, and representations using adjacency lists and matrices. Adjacency lists store the adjacent nodes for each node, while adjacency matrices use a binary matrix to represent edges between nodes. Sparse graphs are typically represented using lists, while dense graphs use matrices.

Uploaded by

makkunda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Graphs: Algorithmic Thinking Luay Nakhleh

The document discusses graphs and their basic definitions and representations. It defines graphs as consisting of nodes and edges, and introduces key graph terminology like degrees, paths, connectivity, and representations using adjacency lists and matrices. Adjacency lists store the adjacent nodes for each node, while adjacency matrices use a binary matrix to represent edges between nodes. Sparse graphs are typically represented using lists, while dense graphs use matrices.

Uploaded by

makkunda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Graphs

Algorithmic Thinking
Luay Nakhleh
Department of Computer Science
Rice University

Why Graphs?

Biological networks

Maps

Social networks

...

Graphs:
Basic Definitions

The Connectivity of Networks

Common to all networks we saw is that the connectivity of each of


them can be represented via some form of a graph

Directed graph

Undirected graph
1

: node
(or, vertex)

2
{1,2}

: edge

: directed edge

(1,2)
5

Graphs

An undirected graph, or graph for short, G, is a pair (V,E), where

V={0,1,...,n-1} is a nonempty set of nodes, and

E{{i,j}: i,j V} is a set of unordered pairs, each of which corresponds to an undirected


edge in the graph G.

A directed graph, or digraph for short, G, is a pair (V,E), where

V={0,1,...,n-1} is a nonempty set of nodes, and

E(VV) is a set of ordered pairs, each of which corresponds to a directed edge in the
graph G.

IMPORTANT: In this course, graphs have no self-loops or parallel edges,


unless explicitly stated otherwise.

Basic Terminology

Two nodes i and j in a graph G=(V,E) are called adjacent (or


neighbors) in G if there is an edge between i and j; that is, if {i,j}E.

The degree of a node in an undirected graph is the number of edges


incident with it. The degree of node i is denoted by deg(i).

What are the degrees of the nodes?


8

Degree Distribution

Define pk to be the fraction of nodes in the graph that have degree k.

The degree distribution of a graph can be visualized by making a


histogram of the pk values.

Degree Distribution

a
p0=1/7
p3=1/7

p1=1/7 p2=1/7
p4=3/7

Notice that if m is the highest node degree, then:


m
X

pk = 1

k=0
10

Basic Terminology

If e=(i,j) is a directed edge from node i to node j, we say that i is the


tail (or, initial node) of e and j is the head (or, terminal node) of e.

The in-degree of a node i in a directed graph is the number of edges


whose head is the node i. The in-degree of node i is denoted by
indeg(i).

The out-degree of i, denoted by outdeg(i), is the number of edges


whose tail is the node i.
b
c
d

What are the in- and out-degrees of the nodes?


11

In- and Out-Degree Distributions

For in-degree distribution: Define pk to be the fraction of nodes in the


graph that have in-degree k.

For out-degree distribution: Define qk to be the fraction of nodes in


the graph that have out-degree k.

The in- and out-degree distributions of a graph can be visualized by


making a histogram of the pk and qk values, respectively.

12

Graph Representation:
Adjacency Lists

Adjacency lists specify the nodes that are adjacent to each node of the
graph.
Node
a
b
c
d
e

Adjacent nodes
b, c, e
a
a, d, e
c, e
a, c, d

While adjacency list is a historical name, the adjacent nodes


of a given node form a set; so, you can think of this representation
as an adjacency set.
13

Graph Representation:
Adjacency Lists

Adjacency lists specify the nodes that are adjacent to each node of the
graph.
Node
a
b
c
d
e

Adjacent nodes
b, c, e
a
a, d, e
c, e
a, c, d

Notice the redundancy!


(ensures symmetry)
In the case of digraphs,
there is no redundancy.

While adjacency list is a historical name, the adjacent nodes


of a given node form a set; so, you can think of this representation
as an adjacency set.
13

Adjacency Lists and Their


Dictionary Representation

Node
a
b
c
d
e

Adjacent nodes
b, c, e
a
a, d, e
c, e
a, c, d

{a: set([b,c,e]),
b: set([a]),
c: set([a,d,e]),
d: set([c,e]),
e: set([a,c,d])}

14

Graph Representation:
Adjacency Matrices

Let G=(V,E) be a graph with V={0,1,...,n-1}

The adjacency matrix of G, denoted by AG, is the nn 0-1 matrix with


1 as its (i,j)th entry when i and j are adjacent, and 0 as its (i,j)th entry
when i and j are not adjacent.
Draw a graph whose
adjacency matrix is

AG

Notice the redundancy!


In the case of digraphs, AG is not necessarily
symmetric.
15

Trade-offs Between Adjacency


Lists and Adjacency Matrices

When the graph is sparse, i.e., contains relatively few edges (relative
to what?), it is usually preferable to use adjacency lists (Why?)

When the graph is dense, i.e., contains relatively many edges (again,
relative to what?), it is usually preferable to use adjacency matrices
(Why?)

16

Graph Connectivity:
Paths

Let k be a nonnegative integer and G a graph.

A path of length k from node v0 to node vk in G is a sequence of k edges


e1,e2,...,ek of G such that e1={v0,v1}, e2={v1,v2}, ..., ek={vk-1,vk}, where v0,...,vk are all
nodes in V, and e1,...,ek are all edges in E.

We usually denote such a path by its node sequence (v0,v1,...,vk).

A path is simple if it does not contain the same node more than once.

A cycle is a simple path that begins and ends at the same node.

A path (not necessarily simple) that begins and ends at the same node is called a
circuit.
17

Graph Connectivity:
Paths

If G is a directed graph, a path must traverse edges in their respective


directions.
Show a simple path from node e to node d in each of
the following two graphs:

b
c

18

Graph Connectivity

An undirected graph is called connected if there is a path between


every pair of distinct nodes of the graph.

A connected component (CC) of a graph G is a connected subgraph of


G that is not a proper subgraph of another connected subgraph of G.

H1
H2
c

H3
e

A graph with 3 CCs


19

Graph Connectivity

A digraph is strongly connected if there is a path from i to j for every pair of nodes i
and j of the digraph (note that there must be a path from i to j and another from j to i).

A digraph is weakly connected if there is a path between every two nodes in the
underlying undirected graph.

The subgraphs of a directed graph G that are strongly connected but not contained in
larger strongly connected subgraphs are called the strongly connected components
(SCC) of G.

c
d

c
d

Strongly connected? Weakly connected? What are the SCCs?


20

You might also like