Docsity

Graph Algorithms: Basics and Implementations, Essays (university) of Computer Science

Partial preview of the text

Download Graph Algorithms: Basics and Implementations and more Essays (university) Computer Science in PDF only on Docsity!

Data Structures and

Algorithms

Chapter 14

Graph Algorithms

Basics

•^
A

graph

is a set

V

of

vertices

and a collection

E

of

edges, G

V
,^ E
•^

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:

Graph Algorithms

Basics

•^

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.

Graph Algorithms

Basics

•^

Parallel edges and self-loops

Graph Algorithms

Basics

•^

Connected graph

and

strongly connected graph

Graph Algorithms

Basics

•^

Subgraph

and

spanning subgraph

Graph Algorithms

Basics

•^

Graph properties– If a graph

G

V

,^

E

) has

m

edges, then

m

  • If

G

V

,^

E

) is a directed graph with

m

edges,

then

•^

Let

G

be a simple graph with

n

vertices and

m

edges.

  • If
G

is undirected, then

  • If
G

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 Algorithms

Basics

•^

Graph properties (continued)– Let

G

be an undirected graph with

n

vertices and

m

edges:• If

G

is connected, then

m

n

  • If
G

is a tree, then

m

n

  • If
G

is a forest, then

m

n

Graph Algorithms

Graph ADT

•^

Oprerations (continued)– outDegree(

v )

  • indegree(

v )

  • outgoingEdges(

v )

  • incomingEdges(

v )

  • insertVertex(

x )

  • insertEdge(

u

,^ v

,^ x

  • removeVertex(

v )

  • removeEdge(

e )

Graph Algorithms

Data Structures for Graphs

•^

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)

Graph Algorithms

Data Structures for Graphs

•^

Adjacency list

-^

V

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

Graph Algorithms

Data Structures for Graphs

•^

Adjacency map

-^

Incidence collection of

V

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 Algorithms

Graph Traversals

•^
A

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

  • Find all vertices of
G

that are reachable from a given

vertex

s

  • Determine whether
G

is acyclic.

  • Determine whether
G

is strongly connected.

Graph Algorithms

Graph Traversals

•^

Applications (for undirected graph):– Find a path from vertex

u

to vertex

v

  • Given a start vertex

s

, find a path with the minimum

number of edges from

s

to every other vertex.

  • Test whether
G

is connected.

  • Find a spanning tree of
G
  • Identify a cycle in
G
•^

Will discuss

depth-first search

DFS

) and

bread-first

search

BFS