Dr Andrew Wallace PhD BEng hons EurIng andrewwallacecsumuse Overview Sets Implementation Complexity Graphs Constructing Graphs Graph examples Sets Collection of items No specified ordered ID: 272555
Download Presentation The PPT/PDF document "Graphs and Sets" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Graphs and Sets
Dr. Andrew Wallace PhD
BEng
(hons)
EurIng
andrew.wallace@cs.umu.seSlide2
Overview
Sets
Implementation
ComplexityGraphsConstructing GraphsGraph examplesSlide3
Sets
Collection of items
No specified ordered
Unique valuesImplementation of mathematical concept of finite setStatic of dynamicSlide4
Sets
Items in a set are
members
of the set No sets of sets but …
Subsets
Union of sets
Intersections
Slide5
Sets
Examples:
int
, float, charArraysFunctionsObjects (struct)Slide6
Set operations
Create
Insert
RemoveIs member ofIs emptySelectSize ofEnumerateSlide7
Implementation
Simple
Array
ListEfficientTreesRadix treesHash tablesSlide8
Implementation
Insert
Check for duplicates
UnionIf list has duplicatesCheck when doingEqualRemoveIntersectionDifferenceSlide9
Implementation
Bit vector
0 or 1
Set the bit if the item is in the queueFaster operationsBit operations in hardware (bitwise AND or OR)Masking0010110101 AND 0010000000Minimise memory
Slide10
Implementation
Bit field
struct
bField{ int
: 6
;
int
m_nVal1 : 3; int m_nVal2 : 4; int m_nVal3 : 6;}Slide11
Implementation
Limitations
Can’t
use bit field variables in an arrayCan’t take the memory address of a bit field variableCan’t overlap integer boundariesSlide12
Implementation
Priority Queues
Linux kernel
Caching algorithms / memory pagesSlide13
Complexity
Depends on implementation
Improve set operations such as union or intersection
Improve insert, search, removeO(n) or O(logn
)
Some set operations can take O(
m
*
n)Slide14
Graphs
Set of nodes or vertices + pairs of nodes
G
= (V, A)Directed or undirectedUndirected a to b is the same as b to aNode - undirectedVertices - directed
Edge
Arcs (directed)
Connection between nodes
Weighted or
unweightedacbdSlide15
Graphs
V = {a, b, c, d}
A = {(a, b), (a, c), (b, d), (c, b)}
Adjacency2 edges are adjacent if the share a common vertex(a, c) and (a, b)2 vertices are adjacent if they share a common edgea and cJoin
Incident
An edge and vertex on the edge
a
c
bdSlide16
Graphs
struct
Node
{ int nNodeID;
Node*
pOut
;
int nOut;};Slide17
Graphs
s
truct
Edge{ int nEdgeID;
int
nStart
; int nEnd;}; Slide18
Graphs
Trivial graph
One vertex
Edgeless graphVertices and no edgesNull graphEmpty setSlide19
Graphs
Paths
Sequence of nodes
{a, b, d}Simple pathNo repetition of nodesCyclic pathAround and around in circles!
Walk
Open walk = path
Closed walk = cyclic path
Trail = walk with unique edges
acbdSlide20
Graphs
Connected graph
All nodes have a path to all other nodes
Sub graphsVertices are a sub set of GAdjacency relationship are a subset of G’s and restricted to the subgraphComplete graph
All nodes connected to all other nodes
Undirected graph A = n(n-1)/2
If A < n-1, then the graph is not connected
a
cbd
c
b
aSlide21
Graphs
Weighted graphs
Maps
Places as nodeRoads as arcsDistances as weights on the edgesSlide22
Graphs
Weights can represent “cost”
Some algorithms require restrictions on weights
Rational numbers or integersAll positive integersWeight of a pathSum of the weights for a given path
a
c
b
d
10
20
23
13Slide23
Constructing Graphs
Adjacency lists
An array of arrays of adjacent vertices
ac
b
d
a
b
d
c
c
b
d
bSlide24
Constructing Graphs
int
**
pNodeArray;pNodeArray = (int**)
malloc
(
sizeof
(
int) * 10);for(i=0;i<10;i++){ pNodeArray[i
] = malloc
(sizeof
(int) *
NumNodesOut[
i
]);
}
pNodeArray
[
i
][j] =
nNodeOut
;Slide25
Constructing Graphs
Adjacency matrix
Node x node matrix (
n x n)a
c
b
d
1
1
1
1Slide26
Constructing Graphs
Undirected graph
symmetrical
ac
b
d
1
1
1
1
1
1
1
1Slide27
Constructing Graphs
int
**
pNodeArray;pNodeArray = (int**)
malloc
(
sizeof
(
int) * 10);for(i=0;i<10;i++){ pNodeArray[i] = malloc
(sizeof
(int)
* 10);
}
pNodeArray
[
i
][j] =
1;Slide28
Graph examples
Robot navigation
AGV (automatic Guided Vehicles)
Free space pathsPick up and drop off pointsMap as a graphSlide29
Graph exampleSlide30
Graph example
Computer NetworksSlide31
Questions?