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

Unit 5 Graphs

The first graph is a simple graph because: - There are no multiple edges between the same pair of vertices - There are no loops (edges that start and end at the same vertex) - The graph is undirected - The graph is unweighted The second graph is not a simple graph because: - There is a loop on vertex 1 - The graph has weighted edges

Uploaded by

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

Unit 5 Graphs

The first graph is a simple graph because: - There are no multiple edges between the same pair of vertices - There are no loops (edges that start and end at the same vertex) - The graph is undirected - The graph is unweighted The second graph is not a simple graph because: - There is a loop on vertex 1 - The graph has weighted edges

Uploaded by

sheren mohamed
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

Discrete Mathematics Computer Science

Level 3

UNIT 5
GRAPHS AND TREES
What is a graph?
A data structure that consists of a set of nodes
(vertices) and a set of edges that relate the nodes to
each other
The set of edges describes relationships among the
vertices
Formal definition of graphs

A graph G is defined as follows:


G=(V,E)
V(G): a finite, nonempty set of vertices
E(G): a set of edges (pairs of vertices)
Directed vs. undirected graphs

When the edges in a graph have no direction,


the graph is called undirected
Directed vs. undirected graphs (cont.)

When the edges in a graph have a direction, the


graph is called directed (or digraph)

Warning: if the graph is


directed, the order of the
vertices in each edge is
important !!

E(Graph2) = {(1,3) (3,1) (5,9) (9,11)


(5,7)
Trees vs graphs
Trees are special cases of graphs!!
Graph terminologies
Adjacent nodes: two nodes are adjacent if they
are connected by an edge
5 is adjacent to 7
7 is adjacent from 5

Path: a sequence of vertices that connect two


nodes in a graph
Complete graph: a graph in which every vertex
is directly connected to every other vertex
Graph terminologies (cont.)
What is the number of edges in a complete
directed graph with N vertices? 
N * (N-1)

2
O( N )
Graph terminology (cont.)
What is the number of edges in a complete
undirected graph with N vertices? 
N * (N-1) / 2
2
O( N )
Graph terminologies (cont.)
Weighted graph: a graph in which each edge
carries a value
Graph implementation
1. Array-based implementation
◦ A 1D array is used to represent the vertices
◦ A 2D array (adjacency matrix) is used to represent
the edges [Implement the following graph into an
array-based]
Array-based implementation
A Graph and its adjacency Matrix
Q: Implement the following graph into
adjacency matrix
Graph implementation (cont.)
2. Linked-list implementation
Q: Implement the following graph into a linked list
◦ A 1D array is used to represent the vertices
◦ A list is used for each vertex v which contains the
vertices which are adjacent from v (adjacency list)
Linked-list implementation
A graph and its Adjacency List
Q:Implement the following graph into adjacency
list
Adjacency matrix vs. adjacency list
representation
Adjacency matrix
◦ Good for dense graphs
◦ Memory requirements: O(|V| + |E| ) = O(|V|
2
)
◦ Connectivity between two vertices can be
tested quickly
Adjacency list
◦ Good for sparse graphs
◦ Memory requirements: O(|V| + |E|)=O(|V|)
◦ Vertices adjacent to another vertex can be
found quickly
Degree of the Graph
G:Given the following graph, write the degree of
each vertix

In graph theory, the degree of a vertex of a graph is


the number of edges incident to the vertex, with
loops counted twice
Simple Graph Also called strict graph

Characteristics of Simple Graph


No two edges correspond to the same pair of vertex
No loops
Undirected
Unweighted
Q: Show if the following graphs are simple
and why

You might also like