Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

MATH1920-Graphs, Paths and Circuits PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Mathematics for Information Technology II(MATH1920) Graphs, Paths and Circuits

Graphs
Applications of Graphs:
1. Facebook/ networking websites/social media: Each user is
represented as a vertex and there is an edge between two friends.
2. Google Maps/GPS/Yahoo maps: To find the shortest path
between various locations. Various locations are represented as
vertices and the roads are represented as edges.
3. Google search: There is a hyperlink between two pages on the
internet. Each page is a vertex and the link between two pages is
an edge.
4. Recommendations on e-commerce websites: Consumer-product
pair use graph theory
5. Chemistry: To study molecules
6. Travelling salesman problem: To find connected components in
a graph.
7. Traversal algorithm is used to find shortest path between two
vertices
8. Transportation networks: Highway networks and flight networks
9. Computer network: Local area network, Internet and Web
10. Databases: For representing Entity Relationship(ER)
diagrams in databases.
11. Flight booking portals uses Graph Theory to find out
shortest and cheapest path for you.
12. Finance [http://jonathankinlay.com/2016/09/applications-
graph-theory-finance/]
[Applications of graphs: https://www.youtube.com/watch?v=YONOwpx00MU ]

1
Mathematics for Information Technology II(MATH1920) Graphs, Paths and Circuits

Graph: A graph is a set of points (vertices) together with a set


of lines (edges). A graph G = (V, E) consists of a pair of sets
V(vertices) and E(edges).
Graphs: Basic Concepts
https://www.youtube.com/watch?v=KgfRjxVRojc (Note: Do
not study isomorphic graphs and planar graphs from the video.)
Link: Definitions, types and examples of graphs:
http://www.personal.kent.edu/~rmuhamma/GraphTheory/MyGraphTheory/defEx.htm ]
Simple graph: A graph with no loops or multiple edges is
called simple.

Multigraph: The graph with loops and parallel edges is called a


multigraph.

2
Mathematics for Information Technology II(MATH1920) Graphs, Paths and Circuits

Digraph/Directed graph: A Graph with directions is called


digraph.

Question: Identify the degree of the vertices of the simple


graph, classify vertices as either even or odd, and give the order
of the graph. Find the total degree of the simple graph.

Solution:
Vertex Degree Even/Odd
A 2 Even
B 3 Odd
C 2 even
D 4 even
E 1 Odd

3
Mathematics for Information Technology II(MATH1920) Graphs, Paths and Circuits

Order of the graph = Number vertices = 5


Total degree of the graph = deg(v1) + deg(v2) + … + deg(vn)
= 2(no. of edges of the graph) = 2+3+2+4+1= 12

Question: Identify the degree of the vertices of the graph,


classify vertices as either even or odd, and give the order of the
graph. Find the total degree of the graph.

Solution:
Vertex Degree Even/Odd
A 4 even
B 4 even
C 4 even
D 4 even
E 4 even
Order of graph = Number of vertices = 5
Total degree of the graph = 20
Question: Identify the degree of the vertices of the multigraph,
classify vertices as either even or odd, and give the order of the
graph. Find the total degree of the graph.

4
Mathematics for Information Technology II(MATH1920) Graphs, Paths and Circuits

Solution:
Vertex Degree Even/Odd
A 2 Even
B 2 Even
C 5 Odd
D 3 Odd
E 4 Even
F 3 Odd
G 1 Odd
Total degree of the graph = deg(v1) + deg(v2) + … + deg(vn)
= 2(no. of edges of the graph)
Total Degree of the graph = 2+2+5+3+4+3+1 = 2(10) = 20
Links:
Graphs and Trees: Find theory and questions related to the
graphs and trees at the following link:
http://cglab.snu.ac.kr/lectures/091/discrete_math/dm09_slide5.pdf
or open website http://cglab.snu.ac.kr , then, click on spring 2009 discrete
mathematics> graphs and trees in order to find the material.

5
Mathematics for Information Technology II(MATH1920) Graphs, Paths and Circuits

Question: Find the adjacency matrix and adjacency list of the


graph.

Solution:
A B C
A 0 1 1
B 1 0 1
C 1 1 0

Adjacency matrix is:


0 1 1
M = [1 0 1]
1 1 0
Adjacency list:
Vertex Adjacent Vertices
A B, C
B A, C
C A, B
Question: Find the adjacency matrix of the graph.

6
Mathematics for Information Technology II(MATH1920) Graphs, Paths and Circuits

Solution: Adjacency matrix:


0 1 1 1 1
1 1 1 1 0
M= 1 0 1 1 1
1 1 0 1 1
[1 1 1 0]1
Question: Find the adjacency matrix of the multigraph.

Solution:

7
Mathematics for Information Technology II(MATH1920) Graphs, Paths and Circuits

OR
0 1 0 1
𝐴 = [1 2 2 1]
0 2 0 0
1 1 0 2
Question: Find the adjacency matrix of the multigraph.

Solution:
2 1 0 1
𝐴 = [1 0 1 0]
0 1 2 2
1 0 2 0

Question: Find the graph if adjacency matrix is given as


2 1 0 1
𝐴 = [1 2 2 1]
0 2 2 0
1 1 0 2
Where each loop counts two for the adjacency matrix element.
Solution:
8
Mathematics for Information Technology II(MATH1920) Graphs, Paths and Circuits

Link: Adjacency Matrix


https://www.youtube.com/watch?v=9C2cpQZVRBA&t=509s
(Study only adajacency matrix for an unweighted graph from the
video)
http://mathworld.wolfram.com/AdjacencyMatrix.html
https://proofwiki.org/wiki/Definition:Adjacency_Matrix (Simple
and multigraph adjacency matrices)
Question: Sketch an example of a graph having five vertices,
three of them are even.

Solution:

9
Mathematics for Information Technology II(MATH1920) Graphs, Paths and Circuits

Question: Sketch a complete graph four -regular graph of order


five.
Solution:

Four-regular graph of order five: Five vertices and four edges


entering each vertex.
Connected Graph: A graph is connected if there is a path
between any two vertices of the graph.
Question:
Determine if the graph is connected or disconnected.

Solution: Connected

Links: Find more about the graphs theory, connected and


disconnected graphs at the link below:
http://mathworld.wolfram.com/ConnectedGraph.html
http://mathworld.wolfram.com/DisconnectedGraph.html

10
Mathematics for Information Technology II(MATH1920) Graphs, Paths and Circuits

Question: Determine if the graph is connected or disconnected

Solution: Disconnected
Walk, Trail and Path Table:
Repeated Repeated Starting and One
Edge Vertex Ending point: Edge(must)
Same
Walk allowed allowed allowed No
Trail No allowed allowed No
Path No no no No
Closed walk allowed allowed yes No
Circuit No allowed yes Yes
Simple Circuit No First and last yes Yes
only

Question: In the graph below, determine which of the following


walks are trails, paths, circuits, or simple circuits.
a) 𝑣1 𝑣2 𝑣3 𝑣3 𝑣4
b) 𝑣1 𝑣2 𝑣3 𝑣4 𝑣3 𝑣5
c) 𝑣2 𝑣3 𝑣4 𝑣5 𝑣3 𝑣6 𝑣2
d) 𝑣2 𝑣3 𝑣4 𝑣5 𝑣6 𝑣2

11
Mathematics for Information Technology II(MATH1920) Graphs, Paths and Circuits

e) 𝑣1 𝑣2 𝑣1
f) 𝑣1
g) 𝑣2 𝑣3 𝑣4 𝑣5 𝑣6

Solution:
a Walk: Yes Trail: Yes(v1 Path:No(repeated
to v4) edge)
b Walk: Trail: Path: No
Yes(v1 to No(Repeated
v5) Edge)
c Walk: Yes Trail: Yes Path: No
d Walk: Yes
e Closed walk: Trail: Yes Path: No
Yes
f Closed walk: Trail: Yes Circuit: No
Yes Path: No
g Walk: Yes Trail: Yes Path: Yes

12
Mathematics for Information Technology II(MATH1920) Graphs, Paths and Circuits

Question: In the following weighted graphs, find the shortest


path from S to T.

Solution: Length of the path S, A, C, E, T =


SA+AC+CE+ET=2+5+2+5=14
Length of the path S, A, D, F, T= 2+3+4+4 = 13
Length of the path S, B, D, F, T = 4+2+4+4=14
Length of the path S, A, D, E, T = 2+3+1+5 = 11
Shortest path is S, A, D, E, T (SADET) with length 11.
Question: In the following weighted graphs, find the minimum
cost of travelling from the city A to D. In this graph vertices
represent cities and edges represent flights.

13
Mathematics for Information Technology II(MATH1920) Graphs, Paths and Circuits

[Reference: http://optlab.mcmaster.ca/feng/4O03/Trip.Planning.pdf]

Solution: $225(show the path)


Question: In the following weighted graphs, find the minimum
cost of travelling from the city 1 to 5. In this graph vertices
represent cities and edges represent the cost of flights in dollars.

[Reference: http://www.ifp.illinois.edu/~angelia/ge330fall09_shortpath_l17.pdf ]

Solution: 10000(please check all the routes)


Euler’s Graph: If a graph contains a walk that traverses each
edge exactly once, goes through all the vertices, and ends at the
starting point, then the graph is said to be Eulerian. That is, it
contains an Eulerian trail.
Link: Euler’s Path and Euler’s Circuit
https://www.youtube.com/watch?v=5M-m62qTR-s

14
Mathematics for Information Technology II(MATH1920) Graphs, Paths and Circuits

Question: Does the graph shown in contain an Euler path? What


about an Euler circuit? If so, find them.

Solution: If we traverse the path B, C, D, E, B, A is a Euler’s


path which is reaching all the vertices and traversing all the
edges only once.
No Euler circuit is possible for the graph because of the position
of the vertex A. We cannot start and end at the same vertex.
Question: Find an Euler path and an Euler’s circuit if exist.

Solution: First Euler’s path: A, B, G, F, E, C, D, G, H, E, A


OR
Second Euler’s path: A, B, G, H, E, F, G, D, C, E, A
Euler circuit exists. Any one out of the above Euler’s paths is an
Euler’s circuit.

15
Mathematics for Information Technology II(MATH1920) Graphs, Paths and Circuits

A graph with no odd vertices (degree: odd number) has at least


one Euler’s path, which will also an Euler’s circuit (Euler’s
Theorem).
Trees
For any positive integer n, any tree with n vertices has n−1
edges. A tree is a connected graph which has no cycles.
Tree is a nonlinear data structure.

16
Mathematics for Information Technology II(MATH1920) Graphs, Paths and Circuits

Applications of the tree


1. Folders in operating system
2. Linux file system is a tree
3. HTML Document Object Model(DOM): All html text,
attributes are stored in tree called Document Object Model
(DOM).
4. Network routing
5. Syntax tree in computer: In computer each expression is
converted into tree format
6. Auto corrector and spell checker
7. Next moves in games
8. To represent a city region telephone network
9. To implement the undo function in a text editor
10. Company organizational structure: company divisions
and departments
11. Databases use binary tree to store data on the drive
12. Routing algorithms where the next path/route of the
packet is determined

17
Mathematics for Information Technology II(MATH1920) Graphs, Paths and Circuits

Basic Concepts: Root of the tree: A


Internal nodes: B, C, D and F
Leaves: E, N, O, G, H, I, J, K, L, M
Parent of E, F and G: B
Children of A: B, C and D
Siblings of C: H, I and J
Ancestors (nodes which appears on the path of the given node to
the root node) of O: A, B and F
Height of the tree: It is the maximum of all vertex depths.
Height of the above tree = 3

18
Mathematics for Information Technology II(MATH1920) Graphs, Paths and Circuits

Basic Concepts and Applications of the Trees:


https://www.youtube.com/watch?v=CzZ6KodVKJo

Binary Trees: In binary tree each node has at most 2 children(0,


1 and 2).

https://www.youtube.com/watch?v=ClN8oEeWSPg (Note: Watch only first 10 minutes of the video)

Question: a) Find height of vertex A and F of the given tree in


the figure 1.
b) Give the preorder traversal for the tree.

19
Mathematics for Information Technology II(MATH1920) Graphs, Paths and Circuits

c) Give the array representation of the tree.


d) List all sibling pairs (A pair of vertices within a tree having a
common parent vertex are said to be siblings) of the vertices.
e) Identify all ancestors (Any vertex lying between a given
vertex and the root vertex is said to be an ancestor of the given
vertex) of vertex B(if any).
f) Find the height of the tree.
g) Give the postorder traversal for the figure 1 tree.
h) Give the inorder traversal of the tree.

Figure 1
Solution: a) Height of vertex A = 3
Height of vertex F = 2
b) Preorder traversal = R, G, C, D, A, B, H, E, F
c) Array representation: It is a 𝑛 × 3 array having n rows and 3
columns(indicating the vertex, the left child and the right child).
20
Mathematics for Information Technology II(MATH1920) Graphs, Paths and Circuits

Vertex Left Child Right Child


R G H
G C D
D A B
C 0 0
A 0 0
B 0 0
H E F
E 0 0
F 0 0

d) Sibling pairs: A pair of vertices within a tree having a


common parent vertex are said to be siblings of the vertices.

Parent Siblings
R G, H
G C, D
D A, B
H E, F

e) D and G are the ancestors


f) Height of the tree: It is the maximum of all vertex depths.
Height of the tree = 3 (maximum of all vertices depths)
g) Postorder traversal of the tree: C, A, B, D, G, E, F, H, R

21
Mathematics for Information Technology II(MATH1920) Graphs, Paths and Circuits

h) Inorder traversal = C, G, A, D, B, R, E, H, F
Link: Tree Traversals: (Study only Preorder, inorder and
postorder traversals from the following links)
https://www.youtube.com/watch?v=GsrPQ32sPTI
https://www.youtube.com/watch?v=r3xN36so6Jg
https://www.youtube.com/watch?v=z_OwSnFY_5s
https://www.youtube.com/watch?v=9RHO6jU--GU

Huffman Tree: Huffman encoding is a simple example of data


compression: representing data in fewer bits than it would
otherwise need

Question: Using the Huffman tree in the figure below, produce


the binary strings corresponding to the following English words.

a) TIN
b) PIN
Solution: a) 11101110
b) 00101110
Question: Find the word represented by the binary strings

22
Mathematics for Information Technology II(MATH1920) Graphs, Paths and Circuits

a) 11100110
b) 111110
c) 100001

Solution: a) 11100110 = PIG


b)111110 = PG
c) 100001 = KIL

Assigning 1 to the left branches and 0,s to the right branches we


get(we can also assign 0 to left and 1 to right branches):

23
Mathematics for Information Technology II(MATH1920) Graphs, Paths and Circuits

Link: Huffman Tree:


http://people.cs.pitt.edu/~kirk/cs1501/animations/Huffman.ht
ml

Home work/practice problems: Odd numbered questions of


Chapter 14(14.1, 14.2 and 14.4).
References:
1) Mathematics for information technology by Alfred Basta,
Stephan Delong and Nadine Basta Cengage Learning
2) Graphs and Trees Theory and Practice problems:
http://cglab.snu.ac.kr/lectures/091/discrete_math/dm09_slide5.p
df
3) Graphs Basic Concepts:
https://cs.hse.ru/data/2015/04/23/1095251484/Lecture%209.1.p
df
4) Graphs: Real Life Examples:
http://software.ucv.ro/~cbadica/pt/chap6.pdf
5) Binary Trees:
https://www.youtube.com/watch?v=ClN8oEeWSPg

24

You might also like