Date post: | 24-Dec-2015 |
Category: |
Documents |
Upload: | ambrose-reed |
View: | 224 times |
Download: | 1 times |
Overview
• Sets
• Implementation
• Complexity
• Graphs
• Constructing Graphs
• Graph examples
Sets
• Collection of items
• No specified ordered
• Unique values
• Implementation of mathematical concept of finite set
• Static of dynamic
Sets
• Items in a set are members of the set
• No sets of sets but …
• Subsets
• Union of sets
• Intersections
Sets
• Examples:• int, float, char• Arrays• Functions• Objects (struct)
Set operations
• Create
• Insert
• Remove
• Is member of
• Is empty
• Select
• Size of
• Enumerate
Implementation
• Simple• Array• List
• Efficient• Trees• Radix trees• Hash tables
Implementation
• Insert• Check for duplicates• Union
• If list has duplicates• Check when doing
• Equal• Remove• Intersection• Difference
Implementation
• Bit vector• 0 or 1• Set the bit if the item is in the queue
• Faster operations• Bit operations in hardware (bitwise AND or OR)• Masking
• 0010110101 AND 0010000000
• Minimise memory
Implementation
• Bit field
struct bField
{
int : 6;
int m_nVal1 : 3;
int m_nVal2 : 4;
int m_nVal3 : 6;
}
Implementation
• Limitations
• Can’t use bit field variables in an array
• Can’t take the memory address of a bit field variable
• Can’t overlap integer boundaries
Implementation
• Priority Queues• Linux kernel
• Caching algorithms / memory pages
Complexity
• Depends on implementation• Improve set operations such as union or intersection• Improve insert, search, remove
• O(n) or O(logn)
• Some set operations can take O(m*n)
Graphs
• Set of nodes or vertices + pairs of nodes• G = (V, A)
• Directed or undirected• Undirected a to b is the same as b to a• Node - undirected• Vertices - directed
• Edge• Arcs (directed)• Connection between nodes• Weighted or unweighted
a
c
b
d
Graphs
• V = {a, b, c, d}
• A = {(a, b), (a, c), (b, d), (c, b)}
• Adjacency• 2 edges are adjacent if the share a common vertex• (a, c) and (a, b)• 2 vertices are adjacent if they share a common edge
• a and c• Join
• Incident• An edge and vertex on the edge
a
c
b
d
Graphs
struct Node
{
int nNodeID;
Node* pOut;
int nOut;
};
Graphs
struct Edge
{
int nEdgeID;
int nStart;
int nEnd;
};
Graphs
• Trivial graph• One vertex
• Edgeless graph• Vertices and no edges
• Null graph• Empty set
Graphs
• Paths• Sequence of nodes• {a, b, d}
• Simple path• No repetition of nodes
• Cyclic path• Around and around in circles!
• Walk• Open walk = path• Closed walk = cyclic path• Trail = walk with unique edges
a
c
b
d
Graphs
• Connected graph• All nodes have a path to all other nodes
• Sub graphs• Vertices are a sub set of G• Adjacency relationship are a subset of G’s and
restricted to the subgraph
• Complete 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
c
b
d
c
b
a
Graphs
• Weighted graphs• Maps• Places as node• Roads as arcs• Distances as weights on the edges
Graphs
• Weights can represent “cost”
• Some algorithms require restrictions on weights• Rational numbers or integers• All positive integers
• Weight of a path• Sum of the weights for a given path
a
c
b
d
10
20
23
13
Constructing Graphs
• Adjacency lists• An array of arrays of adjacent vertices
a
c
b
da
b
d
c
c b
d
b
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;
Constructing Graphs
• Adjacency matrix
• Node x node matrix (n x n)
a
c
b
d
1 1
1
1
Constructing Graphs
• Undirected graph• symmetrical
a
c
b
d
1 1
1
1
1 1
1
1
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;
Graph examples
• Robot navigation
• AGV (automatic Guided Vehicles)• Free space paths• Pick up and drop off points• Map as a graph
Graph example
Graph example
• Computer Networks
Questions?