0% found this document useful (0 votes)
1 views

Graphs

The document provides an overview of graph theory, including definitions, terminology, and properties of graphs such as directed and undirected graphs, cycles, paths, and trees. It also discusses graph representations like adjacency matrices and lists, as well as algorithms for graph traversal, minimum spanning trees, and topological ordering. The content is aimed at enhancing understanding of graph structures and their applications in problem-solving.

Uploaded by

Gaurav Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Graphs

The document provides an overview of graph theory, including definitions, terminology, and properties of graphs such as directed and undirected graphs, cycles, paths, and trees. It also discusses graph representations like adjacency matrices and lists, as well as algorithms for graph traversal, minimum spanning trees, and topological ordering. The content is aimed at enhancing understanding of graph structures and their applications in problem-solving.

Uploaded by

Gaurav Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 77

COMP 6481

Programming
and
Problem Solving
Graphs
Graphs
Terminology
Definition (undiredted, unweighted):
◦ A Graph G, consists of
◦ a set of vertices, V
◦ a set of edges, E
◦ where each edge is associated with a pair of vertices.
We write: G = (V, E)
A labeled simple graph:
Vertex set V = {1, 2, 3, 4, 5, 6}
Edge set E = {{1,2}, {1,5}, {2,3},
{2,5}, {3,4}, {4,5}, {4,6}}.

University of Central Florida


02
Graphs
Edge Types
Directed edge
– ordered pair of vertices (u,v) flight
– first vertex u is the origin ORD PVD
AA 1206
– second vertex v is the
destination
– e.g., a flight
Undirected edge 849
ORD PVD
– unordered pair of vertices (u,v) miles
– e.g., a flight route
Directed graph
– all the edges are directed
– e.g., route network
Undirected graph
– all the edges are undirected
– e.g., flight network
University of Central Florida
03
Graphs
Terminology
End vertices (or endpoints) of an edge
– U and V are the endpoints of a
Edges incident on a vertex
– a, d, and b are incident on V V
a b
Adjacent vertices h j
– U and V are adjacent U d X Z
Degree of a vertex c e i
– X has degree 5
Parallel edges W g
– h and i are parallel edges f
Self-loop Y
– j is a self-loop
University of Central Florida
04
Graphs
Terminology
Path
V
– sequence of alternating vertices a b
and edges P1
– begins with a vertex U
d X Z
– ends with a vertex P2 h
– each edge is preceded and followed c e
by its endpoints W g
Simple path
– path such that all its vertices and f
edges are distinct Y
Examples
– P1=(V,b,X,h,Z) is a simple path
– P2=(U,c,W,e,X,g,Y,f,W,d,V) is a
path that is not simple
University of Central Florida
05
Graphs
Terminology
Cycle
– circular sequence of alternating
vertices and edges V
– each edge is preceded and followed
a b
by its endpoints d
U X Z
Simple cycle C2 h
– cycle such that all its vertices and e C1
c
edges are distinct W g
Examples
– C1=(V,b,X,g,Y,f,W,c,U,a,↵) is a f
Y
simple cycle
– C2=(U,c,W,e,X,g,Y,f,W,d,V,a,↵) is a
cycle that is not simple
University of Central Florida
06
Graphs
Properties
Property 1 Notation
n number of vertices
Sv deg(v) = 2m m number of edges
Proof: each edge is counted
twice deg(v) degree of vertex v
Property 2 Example
In an undirected graph with – n=4
no self-loops and no
multiple edges – m=6
m ≤ n (n - 1)/2 – deg(v) = 3
Proof: each vertex has degree
at most (n - 1)

What is the bound for a


directed graph?
University of Central Florida
07
Graphs
Road Maps

Nodes

Edges

A portion of a road map. Undirected edges

Southern Illinois University


08
Graphs
Street Maps

A directed graph representing a city's street map. Directed edges

Southern Illinois University


09
Graphs
Path
 A sequence of edges that connect two
vertices in a graph
 In a directed graph the direction of the
edges must be considered
 Called a directed path
 A cycle is a path that begins and ends at
same vertex
 Simple path does not pass through any vertex
more than once
 A graph with no cycles is acyclic
Southern Illinois University
10
Graphs
Terminology
Directed Graph:
◦ Same as above, but where each edge is associated
with an ordered pair of vertices.

A labeled simple graph:


Vertex set V = {2,3,5,7,8,9,10,11}
Edge set E = {{3,8}, {3,10}, {5,11},
{7,8}, {7,11}, {8,9},
{11,2},{11,9},{11,10}}.

University of Central Florida


11
Graphs
Weight
 A weighted graph has values on its edges
 Weights or costs
 A path in a weighted graph also has
weight or cost
 The sum of the edge weights
 Examples of weights
 Miles between nodes on a map
 Driving time between nodes
 Taxi cost between node locations
Southern Illinois University
12
Graphs
Terminology
Weighted Graph:
◦ Same as above, but where each edge also has an
associated real number with it, known as the edge
weight.

A labeled weighted graph:


Vertex set V = {1,2,3,4,5}

University of Central Florida


13
Graphs
Connected Graphs
 A connected graph
 Has a path between every pair of
distinct vertices
 A complete graph
 Has an edge between every pair of
distinct vertices
 A disconnected graph
 Not connected

Southern Illinois University


14
Graphs
Connected Graphs

Undirected Graphs

Southern Illinois University


15
Graphs
Adjacent Vertices
 Two vertices are adjacent in an undirected
graph if they are joined by an edge
 Sometimes adjacent vertices are called
neighbors

Vertex A is adjacent to B,
but B is not adjacent to A.
Southern Illinois University
16
Graphs
Airline Routes
 Note the graph with two subgraphs
 Each subgraph connected
 Entire graph disconnected

Southern Illinois University


17
Graphs
Mazes

(a) A maze; (b) its representation as a graph

Southern Illinois University


18
Graphs
Course Prerequisites

The prerequisite structure for a selection of courses as a


directed graph without cycles
Southern Illinois University
19
Graphs
Graph ADT
Vertices and edges Update methods
– are positions – insertVertex(o): insert a vertex
– store elements storing element o
Accessor methods – insertEdge(v, w, o): insert an
edge (v,w) storing element o
– endVertices(e): an array of
the two endvertices of e – removeVertex(v): remove vertex
v (and its incident edges)
– opposite(v, e): the vertex
opposite of v on e – removeEdge(e): remove edge e
– areAdjacent(v, w): true iff v Iterator methods
and w are adjacent – incidentEdges(v): edges
– replace(v, x): replace incident to v
element at vertex v with x – vertices(): all vertices in the
– replace(e, x): replace graph
element at edge e with x – edges(): all edges in the graph

Southern Illinois University


20
Graphs
Adjacency Matrix

(a) A directed graph and (b) its adjacency matrix

Southern Illinois University


21
Graphs
Adjacency Matrix
 Adjacency matrix uses fixed space
 Depends on number of vertices
 Does not depend on number of edges
 Typically the matrix will be sparse
 Presence of an edge between two vertices
can be known immediately
 All neighbors of a vertex found by scanning
entire row for that vertex
Southern Illinois University
22
Graphs
Adjacency List

Southern Illinois University


23
Graphs
Adjacency List

Southern Illinois University


24
Graphs
Adjacency List
 Represents only edges that originate from
the vertex
 Space not reserved for edges that do not
exist
 Uses less memory than corresponding
adjacency matrix
 Thus more often used than adjacency matrix

NYU Computer Science


25
Graphs
Graph Complement
– The complement of a 1 3 5
graph G is a graph G'
which contains all the 6
vertices of G, but for each 4
edge that exists in G, it is 2
NOT in G', and for each
possible edge NOT in G, it 2 3 4 5 6
IS in G'. 2 3 4 5 6 1 0 1 1 0 1
1 1 0 0 1 0 2 0 1 0 1
2 1 0 1 0 3 0 1 1
3 1 0 0 4 0 0
4 1 1 5 1
5 0

Southern Illinois University


26
Graphs
Trees
 All trees are graphs
 But not all graphs are trees
 A tree is a connected graph without cycles
 Traversals
 Preorder, inorder, postorder traversals are
examples of depth-first traversal
 Level-order traversal of a tree is an example of
breadth-first traversal
 Visit a node
 For a tree: process the node's data
 For a graph: mark the node as visited
Southern Illinois University
27
Graphs
Trees

The visitation order of two traversals;


(a) depth first; (b) breadth first.
Southern Illinois University
28
Graphs
Depth First Search (DFS)

NYU Computer Science


29
Graphs
Depth First Search (DFS)

NYU Computer Science


30
Graphs
Depth First Search (DFS)

NYU Computer Science


31
Graphs
Depth First Search (DFS)

NYU Computer Science


32
Graphs
Depth First Search (DFS)

NYU Computer Science


33
Graphs
Depth First Search (DFS)

NYU Computer Science


34
Graphs
Depth First Search (DFS)

NYU Computer Science


35
Graphs
Depth First Search (DFS)

NYU Computer Science


36
Graphs
Depth First Search (DFS)

NYU Computer Science


37
Graphs
Depth First Search (DFS)

NYU Computer Science


38
Graphs
Depth First Search (DFS)

NYU Computer Science


39
Graphs
Depth First Search (DFS)

NYU Computer Science


40
Graphs
Depth First Search (DFS)

NYU Computer Science


41
Graphs
Breadth First Search (BFS)
 Visit nodes closest to the originating node
before diving down into the tree
 Implemented using Queue

NYU Computer Science


42
Graphs
Breadth First Search (BFS)

NYU Computer Science


43
Graphs
Breadth First Search (BFS)

NYU Computer Science


44
Graphs
Breadth First Search (BFS)

NYU Computer Science


45
Graphs
Breadth First Search (BFS)

NYU Computer Science


46
Graphs
Breadth First Search (BFS)

NYU Computer Science


47
Graphs
Breadth First Search (BFS)

NYU Computer Science


48
Graphs
Breadth First Search (BFS)

NYU Computer Science


49
Graphs
Breadth First Search (BFS)

NYU Computer Science


50
Graphs
Breadth First Search (BFS)

NYU Computer Science


51
Graphs
Graph Traversal Exercises

Breadth-First and Depth-First Traversal starting from a

NYU Computer Science


52
Graphs
Possible Answers
 Breadth-first
 afhegidjkclnbmo
 Depth-first
 afedcbghijklmno

NYU Computer Science


53
Graphs
Topological Order
• Given a directed graph without cycles (DAG)
• Topological ordering of vertices in a directed
acyclic graph.
• In a topological order
– Vertex a precedes vertex b whenever
– A directed edge exists from a to b

Southern Illinois University


54
Graphs
Topological Order
• First find any vertex with no incoming edges.
• Print this vertex, and remove it, along with
its edges from the graph.
• Then we apply the same strategy to the rest
of graph.
• Indegree of node: Incoming number of edges

Southern Illinois University


55
Graphs
Topological Order

Three topological
orders for the
graph

Southern Illinois University


56
Graphs
Topological Order

Southern Illinois University


57
Graphs
Possible Answers
• The graph shown has many valid topological
sorts, including:
• 7,5,3,11,8,2,9,10
• 7,5,11,2,3,10,8,9
• 3,7,8,5,11,10,9,2

Southern Illinois University


58
Graphs
Minimum Spanning Trees
 Spanning tree
 Given: a connected, undirected graph G = (V, E)
 (V is the set of vertices, E is the set of edges)
 A spanning tree is a set of edges that is a tree
and “covers” all vertices V
 There can be several trees
 The spanning tree with the minimum cost
(sum of edge weights) is called the Minimum
Spanning Tree

Southern Illinois University


59
Graphs
Minimum Spanning Trees

Southern Illinois University


60
Graphs
Minimum Spanning Trees

Southern Illinois University


61
Graphs
Minimum Spanning Trees

Southern Illinois University


62
Graphs
Minimum Spanning Trees
 Kruskal's algorithm for finding the MST
 Repeatedly finds edges with minimum costs
that does not form a cycle
 Greedy algorithm, provably correct
1) Sort edges by increasing weight
2) While there are unprocessed edges left
1) Pick an edge e with minimum cost
2) If adding e to the MST does not form a
cycle, add e to MST
Southern Illinois University
63
Graphs
Minimum Spanning Trees
 Kruskal's algorithm for finding the MST
 How to store and sort edges?
 Using an edge list and Collections.sort
 How to test for cycles?
 using disjoint sets and union-find

Southern Illinois University


64
Graphs
Minimum Spanning Trees

Southern Illinois University


65
Graphs
Minimum Spanning Trees

Southern Illinois University


66
Graphs
Minimum Spanning Trees

Southern Illinois University


67
Graphs
Minimum Spanning Trees

Southern Illinois University


68
Graphs
Minimum Spanning Trees

Southern Illinois University


69
Graphs
Shortest Paths

From node 5, find # of nodes > 3 hops away

NYU Computer Science


70
Graphs
Shortest Paths

NYU Computer Science


71
Graphs
Shortest Paths

NYU Computer Science


72
Graphs
Shortest Paths

NYU Computer Science


73
Graphs
Shortest Paths

NYU Computer Science


74
Graphs
Shortest Paths

NYU Computer Science


75
Graphs
Shortest Paths

NYU Computer Science


76
Graphs
Dijkstra's in code

NYU Computer Science


77

You might also like