# Graphs and Sets

```Graphs and Sets
Dr. Andrew Wallace PhD BEng(hons) EurIng
andrew.wallace@cs.umu.se
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
• 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)
• 0010110101 AND 0010000000
• Minimise memory
𝒏
•
𝒘
Implementation
• Bit field
struct bField
{
int
int
int
int
}
m_nVal1
m_nVal2
m_nVal3
: 6;
: 3;
: 4;
: 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)
a
• Directed or undirected
b
• Undirected a to b is the same as b to a
• Node - undirected
• Vertices - directed
• Edge
• Arcs (directed)
• Connection between nodes
• Weighted or unweighted
c
d
Graphs
a
b
• V = {a, b, c, d}
• A = {(a, b), (a, c), (b, d), (c, b)}
• 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
c
d
Graphs
struct Node
{
int
Node*
int
};
nNodeID;
pOut;
nOut;
Graphs
struct Edge
{
int
int
int
};
nEdgeID;
nStart;
nEnd;
Graphs
• Trivial graph
• One vertex
• Edgeless graph
• Vertices and no edges
• Null graph
• Empty set
Graphs
a
b
• 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
c
d
Graphs
d
a
b
• 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 &lt; n-1, then the graph is not connected
c
Graphs
• Weighted graphs
•
•
•
•
Maps
Places as node
Distances as weights on the edges
a
10
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
b
20
13
23
c
d
Constructing Graphs
a
b
• An array of arrays of adjacent vertices
a
c
b
c
b
d
c
b
d
d
Constructing Graphs
int**
pNodeArray;
pNodeArray = (int**)malloc(sizeof(int) * 10);
for(i=0;i&lt;10;i++)
{
pNodeArray[i] = malloc(sizeof(int) * NumNodesOut[i]);
}
pNodeArray[i][j] = nNodeOut;
Constructing Graphs
a
b
• Node x node matrix (n x n)
c
1
1
1
1
d
Constructing Graphs
a
b
• Undirected graph
• symmetrical
c
1
1
1
1
1
1
1
1
d
Constructing Graphs
int**
pNodeArray;
pNodeArray = (int**)malloc(sizeof(int) * 10);
for(i=0;i&lt;10;i++)
{
pNodeArray[i] = malloc(sizeof(int) * 10);
}
pNodeArray[i][j] = 1;
Graph examples