Collaborations:
Data Structures:
Trees and Graphs
Md. Tanvir Ahmed (22111128)
Md. Akash Mia (22111125)
Md. Ahsanul Haque Riad (22111127)
Md. Sazid Hossain (22111126)
Najmul Kabir Pollob (22111128)
Trees
2
🞑 A tree isa hierarchical data structure composed of nodes.
🞑 Root: the top-most node (unlike real trees, trees in computer science
grow downward!). Every (non-empty) tree has one.
🞑 Parent: the node connected directly above the current one. Every node
(except for the root) has one.
🞑 Child: a node connected below the current one. Each node can have 0
or more.
🞑 Leaf: a node that has no children.
🞑 Depth/Level: the length of the path (edges) from the root to a node
(depth/level of the root is0).
🞑 Tree Height: the maximum depth from of any node in the tree.
🞑 A tree commonly used in computing is a binary tree.
🞑 A binary tree consists of nodes that have at most 2 children.
🞑 Commonly used in: data compression, file storage, game trees
Binary Tree Example
84
3
65 96
24 50 53
48 37
Which node is the root?
Which nodes are the leaves?
Which nodes are internal nodes?
What is the height of thistree?
Binary Tree Example
84
4
65 96
24 50
48 37
53
The root contains the data value 84.
There are 4 leaves in this binary tree: nodes containing 48, 37, 50, 53.
There are 4 internal nodes in this binary tree: containing 84, 65, 96, 24
This binary tree has height 3 – considering root is at level 0,
the length of the longest path from root to a leaf is 3
Level 0
Level 1
Level 2
Level 3
Binary Trees:A recursive structure!
🞑 The yellow node with the key 65 can be viewed as the
root of the left subtree, which in turn has
🞑 a left subtree of blue nodes
🞑 a right subtree of orange nodes
🞑 In general, Binary Trees can be:
🞑 Empty
🞑 A root node with
🞑 a left binary tree
🞑 a right binary tree
84
5
65 96
24
37
50
48
53
Binary Trees:Implementation
🞑 A common implementation of binary trees usesnodes
🞑 Each node hasa “left” node and a “right” node.
🞑 How to represent these nodes and pointers? With a Class (like a
Struct…)
65
24 50
48 37 14
Level 0
Level 1
Level 2
6
Implement Using a List
🞑 We could also use a list to implement binary trees. For example:
0 1 2 3 4 5 6 7
65
24 50
48 37 14
Level 0
Level 1
Level 2
- 65 24 50 48 37 - 14
7
Binary Search Tree (BST)
8
🞑 A binary search tree (BST)isa binary tree with no
duplicate nodes that imposes an ordering on its nodes.
🞑 BSTordering invariant: At any node n with value k,
🞑 all values of nodes in the left subtree of n are strictly less than
k
🞑 all values of nodes in the right subtree of n are strictly greater
than k
Example:Binary Search Tree
8
4
1 6 9
BST ordering invariant: At any node with value k,
all values of elements in the left subtree are strictly less than k and
all values of elements in the right subtree are strictly greater than k
(assuming that there are no duplicates in the tree)
7
Example:Is thisa BST?
4
7
1
6
9
8
yes
no
3
4
7
1
6
9
8
3
10
Insertion in a BST
11
🞑 For each data value that you wish to insert into the binary
search tree:
🞑 If you reach an empty tree (must test this first, why?), create
a new leaf node with your value at that location
🞑 Recursively search the BSTfor your value until you either find
it or reach an empty tree
🞑 If you find the value, throw an exception since duplicates
are not allowed
Insertion Example
🞑 Insert:84, 41, 96, 24, 37, 50, 13, 98
84
12
41 96
24
37
50
13
98
Binary Search Tree Complexity
6
8
4
9
1 5
1
4
7
6
8
9
Tree 1
13
Tree 2
O(log n) O(n)
Relationship Data
14
From this…
Relationship Data
To this…
A
D
C
B
G
E
F
15
Graphs
16
🞑 A graph is a data structure that contains of a set of vertices
and a set of edges which connect pairsof the vertices.
🞑 A vertex (or node) can be connected to any number of other
vertices using edges.
🞑 An edge may be bidirectional or directed (one-way).
🞑 An edge may have a weight on it that indicates a cost for
traveling over that edge in the graph.
🞑 Unlike trees, graphscan contain cycles
🞑 In fact, a tree is an acyclic graph
🞑 Applications: computer networks, transportation systems,
social networks
Example Graphs
B
D
C
Vertex A
B
A
D
C
Undirected
17
Directed
Graph Implementation
🞑 We usually represent graphs using a table (2d list) where
each column and row isassociated with a specific
vertex. This iscalled an adjacency matrix.
🞑 A separate list of vertices shows which vertex name (city,
person, etc.) is associated with each index
🞑 The values of the 2d list are the weights of the edges
between the row vertices and column vertices
🞑 If there is not an edge between the two vertices, we use
infinity, or None, to represent that
Graph Representation
B
A
D
C
6
4
5
3
7
0 6 7 5
6 0 4 ∞
7 4 0 3
5 ∞ 3 0
A
B
C
D
to
A B C D
0 6 7 5
∞ 0 4 ∞
2 ∞ 0 3
∞ ∞ 9 0
A
B
C
D
to
A B C D
B
A
D
C
6
4
5
3
7
2
9
19
from from
weight
Graph Algorithms
20
🞑 Lots! Here are some examples.
🞑 There are algorithms to search graphs efficiently for a value
🞑 Breadth-first search and Depth-first search
🞑 There are algorithms to compute the shortest path between
a start vertex and all the others
🞑 Dijkstra’s algorithm
🞑 There are algorithms for operations research, which can be
used to solve network flow problems
🞑 For example, how to efficiently distribute water through a system
of pipes
Shortest Path (Dijkstra's Algorithm)
21
🞑 Assign every node an initial distance (0 to the source, ∞for all
others);mark all nodes as unvisited
🞑 While there are unvisited nodes:
🞑 select unvisited node with smallest distance (current)
🞑 consider all unvisited neighbors of current node:
🞑 compute distance to each neighbor from current node
🞑 if less than current distance, replace with new distance
🞑 mark current node as visited (and never evaluate again)
Dijkstra example
B
D E
C
F
6
2
2
19
17
4
7
5
G
A
12
22
3 4
6
A B C D E F G
0 ∞ ∞ ∞ ∞ ∞ ∞
A->
Dijkstra example
B
D E
C
F
6
2
2
19
17
4
7
5
G
A
12
23
3 4
6
A B C D E F G
0 ∞ ∞ ∞ ∞ ∞ ∞
12 3 ∞ 17 ∞ ∞
5 22 17 8 7
11 17 8 7
11 17 8
11 15
13
0 5 3 11 13 8 7
C->
A->
B->
F->
E
G->
D->
What does this assume?
Thank You From CSE 4th Batch

trees-and-graphs_computer_science_for_student.pptx

  • 1.
    Collaborations: Data Structures: Trees andGraphs Md. Tanvir Ahmed (22111128) Md. Akash Mia (22111125) Md. Ahsanul Haque Riad (22111127) Md. Sazid Hossain (22111126) Najmul Kabir Pollob (22111128)
  • 2.
    Trees 2 🞑 A treeisa hierarchical data structure composed of nodes. 🞑 Root: the top-most node (unlike real trees, trees in computer science grow downward!). Every (non-empty) tree has one. 🞑 Parent: the node connected directly above the current one. Every node (except for the root) has one. 🞑 Child: a node connected below the current one. Each node can have 0 or more. 🞑 Leaf: a node that has no children. 🞑 Depth/Level: the length of the path (edges) from the root to a node (depth/level of the root is0). 🞑 Tree Height: the maximum depth from of any node in the tree. 🞑 A tree commonly used in computing is a binary tree. 🞑 A binary tree consists of nodes that have at most 2 children. 🞑 Commonly used in: data compression, file storage, game trees
  • 3.
    Binary Tree Example 84 3 6596 24 50 53 48 37 Which node is the root? Which nodes are the leaves? Which nodes are internal nodes? What is the height of thistree?
  • 4.
    Binary Tree Example 84 4 6596 24 50 48 37 53 The root contains the data value 84. There are 4 leaves in this binary tree: nodes containing 48, 37, 50, 53. There are 4 internal nodes in this binary tree: containing 84, 65, 96, 24 This binary tree has height 3 – considering root is at level 0, the length of the longest path from root to a leaf is 3 Level 0 Level 1 Level 2 Level 3
  • 5.
    Binary Trees:A recursivestructure! 🞑 The yellow node with the key 65 can be viewed as the root of the left subtree, which in turn has 🞑 a left subtree of blue nodes 🞑 a right subtree of orange nodes 🞑 In general, Binary Trees can be: 🞑 Empty 🞑 A root node with 🞑 a left binary tree 🞑 a right binary tree 84 5 65 96 24 37 50 48 53
  • 6.
    Binary Trees:Implementation 🞑 Acommon implementation of binary trees usesnodes 🞑 Each node hasa “left” node and a “right” node. 🞑 How to represent these nodes and pointers? With a Class (like a Struct…) 65 24 50 48 37 14 Level 0 Level 1 Level 2 6
  • 7.
    Implement Using aList 🞑 We could also use a list to implement binary trees. For example: 0 1 2 3 4 5 6 7 65 24 50 48 37 14 Level 0 Level 1 Level 2 - 65 24 50 48 37 - 14 7
  • 8.
    Binary Search Tree(BST) 8 🞑 A binary search tree (BST)isa binary tree with no duplicate nodes that imposes an ordering on its nodes. 🞑 BSTordering invariant: At any node n with value k, 🞑 all values of nodes in the left subtree of n are strictly less than k 🞑 all values of nodes in the right subtree of n are strictly greater than k
  • 9.
    Example:Binary Search Tree 8 4 16 9 BST ordering invariant: At any node with value k, all values of elements in the left subtree are strictly less than k and all values of elements in the right subtree are strictly greater than k (assuming that there are no duplicates in the tree) 7
  • 10.
  • 11.
    Insertion in aBST 11 🞑 For each data value that you wish to insert into the binary search tree: 🞑 If you reach an empty tree (must test this first, why?), create a new leaf node with your value at that location 🞑 Recursively search the BSTfor your value until you either find it or reach an empty tree 🞑 If you find the value, throw an exception since duplicates are not allowed
  • 12.
    Insertion Example 🞑 Insert:84,41, 96, 24, 37, 50, 13, 98 84 12 41 96 24 37 50 13 98
  • 13.
    Binary Search TreeComplexity 6 8 4 9 1 5 1 4 7 6 8 9 Tree 1 13 Tree 2 O(log n) O(n)
  • 14.
  • 15.
  • 16.
    Graphs 16 🞑 A graphis a data structure that contains of a set of vertices and a set of edges which connect pairsof the vertices. 🞑 A vertex (or node) can be connected to any number of other vertices using edges. 🞑 An edge may be bidirectional or directed (one-way). 🞑 An edge may have a weight on it that indicates a cost for traveling over that edge in the graph. 🞑 Unlike trees, graphscan contain cycles 🞑 In fact, a tree is an acyclic graph 🞑 Applications: computer networks, transportation systems, social networks
  • 17.
  • 18.
    Graph Implementation 🞑 Weusually represent graphs using a table (2d list) where each column and row isassociated with a specific vertex. This iscalled an adjacency matrix. 🞑 A separate list of vertices shows which vertex name (city, person, etc.) is associated with each index 🞑 The values of the 2d list are the weights of the edges between the row vertices and column vertices 🞑 If there is not an edge between the two vertices, we use infinity, or None, to represent that
  • 19.
    Graph Representation B A D C 6 4 5 3 7 0 67 5 6 0 4 ∞ 7 4 0 3 5 ∞ 3 0 A B C D to A B C D 0 6 7 5 ∞ 0 4 ∞ 2 ∞ 0 3 ∞ ∞ 9 0 A B C D to A B C D B A D C 6 4 5 3 7 2 9 19 from from weight
  • 20.
    Graph Algorithms 20 🞑 Lots!Here are some examples. 🞑 There are algorithms to search graphs efficiently for a value 🞑 Breadth-first search and Depth-first search 🞑 There are algorithms to compute the shortest path between a start vertex and all the others 🞑 Dijkstra’s algorithm 🞑 There are algorithms for operations research, which can be used to solve network flow problems 🞑 For example, how to efficiently distribute water through a system of pipes
  • 21.
    Shortest Path (Dijkstra'sAlgorithm) 21 🞑 Assign every node an initial distance (0 to the source, ∞for all others);mark all nodes as unvisited 🞑 While there are unvisited nodes: 🞑 select unvisited node with smallest distance (current) 🞑 consider all unvisited neighbors of current node: 🞑 compute distance to each neighbor from current node 🞑 if less than current distance, replace with new distance 🞑 mark current node as visited (and never evaluate again)
  • 22.
    Dijkstra example B D E C F 6 2 2 19 17 4 7 5 G A 12 22 34 6 A B C D E F G 0 ∞ ∞ ∞ ∞ ∞ ∞ A->
  • 23.
    Dijkstra example B D E C F 6 2 2 19 17 4 7 5 G A 12 23 34 6 A B C D E F G 0 ∞ ∞ ∞ ∞ ∞ ∞ 12 3 ∞ 17 ∞ ∞ 5 22 17 8 7 11 17 8 7 11 17 8 11 15 13 0 5 3 11 13 8 7 C-> A-> B-> F-> E G-> D-> What does this assume?
  • 24.
    Thank You FromCSE 4th Batch