0% found this document useful (0 votes)
45 views30 pages

Dastal DEMO

This document discusses data structures and algorithms. It defines a data structure as a way to organize data to facilitate access and modifications. An algorithm is defined as a well-defined computational procedure that takes input and produces output. Graphs and trees are then discussed in detail, including definitions, examples, terminology, applications, and traversal methods for trees.

Uploaded by

teztdummy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views30 pages

Dastal DEMO

This document discusses data structures and algorithms. It defines a data structure as a way to organize data to facilitate access and modifications. An algorithm is defined as a well-defined computational procedure that takes input and produces output. Graphs and trees are then discussed in detail, including definitions, examples, terminology, applications, and traversal methods for trees.

Uploaded by

teztdummy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 30

Alexandro B.

Ponteras
DATA
STRUCTURES &
ALGORITHMS

Graphs & Trees


Data Structures:
A data structure is a way to store and organize data in order
to facilitate access and modifications.

Algorithm
 an algorithm is any well-defined computational procedure that takes
input and produces output.

 A sequence of computational steps that transform the input into the


output.

 A tool for solving a well-specified computational problem.


 The algorithm describes a specific computational procedure for

achieving that input/output relationship.


 e.g.) sorting problem – sorting algorithm
.. continued.
 An algorithm is said to be correct if it produce correct
output for every input instance.
 A correct algorithm solves the given problem.

 An incorrect algorithm might not result at all or

it might result with an answer other than the desired


one.
Outline
 Graphs
 Definition

 Applications

 Terminology

 Digraphs
 Undirected Graph
 Weighted graphs
 Trees
 Definition

 Tree traversal
Graph
 A graph is a pair (V, E), where
 V is a set of nodes, called vertices
 E is a collection of pairs of vertices, called edges
 Vertices and edges are positions and store elements
 Example:
 A vertex represents a town and stores the three-letter town
code
 An edge represents a route between two towns and stores
the mileage of the route
849 SC
1843 SAG 2
BAC 14

7 43 802 KAB
337

1 8 7 10
2555 13
PUL 1233 99
BAG HIN 1120
BIN
Edge Types
 Directed edge
 ordered pair of vertices (u,v)
 first vertex u is the origin
 second vertex v is the
destination trip
BAC AA 1206 SC
 e.g., a trip
 Undirected edge
 unordered pair of vertices
(u,v) 849
BAC SC
 e.g., a trip route miles
 Directed graph
 all the edges are directed
 e.g., trip network
 Undirected graph
 all the edges are undirected
 e.g., route network
Applications
 Electronic circuits cslab1a cslab1b
 Printed circuit board
math.brown.edu
 Integrated circuit

 Transportation networks
cs.brown.edu
 Highway network

 Flight network
brown.edu
 Computer networks qwest.net
 Local area network att.net

 Internet

 Web
cox.net
 Databases John

 Entity-relationship diagram Paul


David
Terminology
 End vertices (or endpoints)
of an edge
 U and V are the endpoints

of a
 Edges incident on a vertex V
 a, d, and b are incident on a b
V h j
 Adjacent vertices U d X Z
 U and V are adjacent c e i
 Degree of a vertex W g
 X has degree 5

 Parallel edges f
Y
 h and i are parallel edges

 Self-loop
 j is a self-loop
Terminology (continued)
 Path
 sequence of alternating

vertices and edges


 begins with a vertex

 ends with a vertex V


a b
 each edge is preceded and
P1
followed by its endpoints
U d X Z
 Simple path P2 h
 path such that all its vertices c e
and edges are distinct W g
 Examples
 P =(V,b,X,h,Z) is a simple
1 f
path Y
 P =(U,c,W,e,X,g,Y,f,W,d,V) is
2
a path that is not simple
Terminology (cont.)
 Cycle
 circular sequence of
alternating vertices and
edges
 each edge is preceded and
V
a b
followed by its endpoints
 Simple cycle U d X Z
 cycle such that all its vertices C2 h
c e C1
and edges are distinct
W g
 Examples
 C =(V,b,X,g,Y,f,W,c,U,a,) is
1
f
a simple cycle Y
 C =(U,c,W,e,X,g,Y,f,W,d,V,a,
2
) is a cycle that is not simple
Digraphs
 A digraph is a graph E
whose edges are all
directed D
 Short for “directed graph”
C

A
Digraph Properties

 A graph G=(V,E) such that


 Each edge goes in one direction:
 Edge (a,b) goes from a to b, but not b to a.

C
B

A
Undirected Graph

 Simply connects two vertices


Weighted Graphs
 In a weighted graph, each edge has an associated
numerical value, called the weight of the edge
 Edge weights may represent, distances, costs, etc.
 Example:
 In a trip route graph, the weight of an edge represents the

distance in miles between the endpoint towns

849 SC
1843 SAG 2
BAC 14

802

1205
43 KAB
17
337

7
PUL 2555 138 10
99
1233
BAG HIN 1120
BIN
Shortest Path Problem
 Given a weighted graph and two vertices u and v, we want to
find a path of minimum total weight between u and v.
 Length of a path is the sum of the weights of its edges.

 Example:
 Shortest path between Pulupandan and San Carlos

849 SC
1843 SAG 2
BAC 14
802

1205
7 43 KAB
337

1 8 7 10
2555 13
PUL 1233 99
BAG HIN 1120
BIN
Tree

 A tree is a non-empty set, one element of


which is designated the root of the tree while
the remaining elements are partitioned into
non-empty sets each of which is a sub tree of
the root.
Example of a Tree
Tree Traversal

 tree-traversal refers to the process of visiting


each node in a tree data structure, exactly
once, in a systematic way. Such traversals
are classified by the order in which the nodes
are visited.
Tree Traversal Order

 preorder
 inorder
 postorder
Preorder traversal

 Visit the root.


 Traverse the left subtree.
 Traverse the right subtree.
Preorder traversal sequence

 F, B, A, D, C, E, G, I, H
Inorder traversal

 Traverse the left subtree.


 Visit the root.
 Traverse the right subtree.
Inorder traversal sequence

 A, B, C, D, E, F, G, H, I
Postorder traversal

 Traverse the left subtree.


 Traverse the right subtree.
 Visit the root.
Postorder traversal sequence

 A, C, E, D, B, H, I, G, F
END

You might also like