















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
















































graph
is a set
of
vertices
and a collection
of
edges, G
An edge connecting vertices (or nodes)
u
and
v
is
denoted (
u ,
v
An edge can be
directed
or
undirected
Directed graph vs. undirected graph:
Outgoing edge vs. incoming edge
-^
Degree, in-degree, and out-degree of a node
-^
The outgoing edges of vertex
g
are (
g ,
e
),
( g
,^ h
).
-^
The incoming edges of vertex
g
are (
a ,
g
),
( b
,^ g
), (
c ,
g
).
-^
The degree of vertex
g
,^ deg
( g
) = 5.
-^
The in-degree of vertex
g
,^ indeg
( g
) = 3.
-^
The out-degree of vertex
g
,^ outdeg
( g
) = 2.
Parallel edges and self-loops
Connected graph
and
strongly connected graph
Subgraph
and
spanning subgraph
m
Let
be a simple graph with
n
vertices and
m
edges.
is undirected, then
is directed, then
m
n
( n
Vin v
v ) deg(
m v
out
v
in
Vin v
Vin v
^
)
deg(
)
deg(
2
) 1 (^
^
n n m
Graph properties (continued)– Let
be an undirected graph with
n
vertices and
m
edges:• If
is connected, then
m
n
is a tree, then
m
n
is a forest, then
m
n
Oprerations (continued)– outDegree(
v )
v )
v )
v )
x )
u
,^ v
,^ x
v )
e )
Edge list, adjacency list, adjacency map, adjacencymatrix Method
EdgeList
Adj. List
Adj. Map
Adj. Matrix
vertices( )
O(n)
O(n)
O(n)
O(n)
edges( )
O(m)
O(m)
O(m)
O(m)
getEdge(u, v)
O(m)
O(min(d
, du
))v
O(1) exp.
O(1)
outDegree(v)inDegree(v)
O(m)
O(1)
O(1)
O(n)
outgoingEdges(v)incomingEdges(v)
O(m)
O(d
)^ v
O(d
)^ v
O(n)
insertVertex(x)
O(1)
O(1)
O(1)
O(n
2 )
removeVertex(v)
O(m)
O(d
)^ v
O(d
)^ v
O(n
2 )
insertEdge(u, v, x)
O(1)
O(1)
O(1) exp.
O(1)
remove Edge(e)
O(1)
O(1)
O(1) exp.
O(1)
Adjacency list
-^
is a list of vertices.
-^
Each vertex
v
has a reference to a separate collection of
edges that are incident to
v
The collection is called
incidence collection
u
w
v
a x
b^
c
d
e
u v w x
a, b, ca, d b, e c, d, e
V
Adjacency map
-^
Incidence collection of
is implemented as a map.
Suppose edge
a
u
,^ v
) is in the incidence collection of
u
. Then, <
v ,
a
pair is stored in the map, where
v
is a
key and
a u^ is the corresponding value.
w
v
a x
b^
c
d
e
u v w x
v ‐> aw ‐> bx ‐> c
u ‐> ax ‐> d
u ‐> bx ‐> e
v ‐> dw ‐> eu ‐> c
V
graph traversal
is a systematic procedure for visiting
(and processing) all vertices in the graph.
-^
We say a traversal is efficient if its running time isproportional to the number of vertices and edges in thegraph.
-^
Applications (for directed graph):– Find a direct path from vertex
u
to vertex
v
that are reachable from a given
vertex
s
is acyclic.
is strongly connected.
Applications (for undirected graph):– Find a path from vertex
u
to vertex
v
s
, find a path with the minimum
number of edges from
s
to every other vertex.
is connected.
Will discuss
depth-first search
) and
bread-first
search