graphs and sets dr. andrew wallace phd beng(hons) euring [email protected]

31
Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng [email protected]

Upload: ambrose-reed

Post on 24-Dec-2015

225 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Graphs and SetsDr. Andrew Wallace PhD BEng(hons) EurIng

[email protected]

Page 2: Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Overview

• Sets

• Implementation

• Complexity

• Graphs

• Constructing Graphs

• Graph examples

Page 3: Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Sets

• Collection of items

• No specified ordered

• Unique values

• Implementation of mathematical concept of finite set

• Static of dynamic

Page 4: Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Sets

• Items in a set are members of the set

• No sets of sets but …

• Subsets

• Union of sets

• Intersections

Page 5: Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Sets

• Examples:• int, float, char• Arrays• Functions• Objects (struct)

Page 6: Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Set operations

• Create

• Insert

• Remove

• Is member of

• Is empty

• Select

• Size of

• Enumerate

Page 7: Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Implementation

• Simple• Array• List

• Efficient• Trees• Radix trees• Hash tables

Page 8: Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Implementation

• Insert• Check for duplicates• Union

• If list has duplicates• Check when doing

• Equal• Remove• Intersection• Difference

Page 9: Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

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

Page 10: Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Implementation

• Bit field

struct bField

{

int : 6;

int m_nVal1 : 3;

int m_nVal2 : 4;

int m_nVal3 : 6;

}

Page 11: Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

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

Page 12: Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Implementation

• Priority Queues• Linux kernel

• Caching algorithms / memory pages

Page 13: Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

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)

Page 14: Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

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

Page 15: Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

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

Page 16: Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Graphs

struct Node

{

int nNodeID;

Node* pOut;

int nOut;

};

Page 17: Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Graphs

struct Edge

{

int nEdgeID;

int nStart;

int nEnd;

};

Page 18: Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Graphs

• Trivial graph• One vertex

• Edgeless graph• Vertices and no edges

• Null graph• Empty set

Page 19: Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

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

Page 20: Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

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

Page 21: Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Graphs

• Weighted graphs• Maps• Places as node• Roads as arcs• Distances as weights on the edges

Page 22: Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

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

Page 23: Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Constructing Graphs

• Adjacency lists• An array of arrays of adjacent vertices

a

c

b

da

b

d

c

c b

d

b

Page 24: Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

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;

Page 25: Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Constructing Graphs

• Adjacency matrix

• Node x node matrix (n x n)

a

c

b

d

1 1

1

1

Page 26: Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Constructing Graphs

• Undirected graph• symmetrical

a

c

b

d

1 1

1

1

1 1

1

1

Page 27: Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

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;

Page 28: Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Graph examples

• Robot navigation

• AGV (automatic Guided Vehicles)• Free space paths• Pick up and drop off points• Map as a graph

Page 29: Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Graph example

Page 30: Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Graph example

• Computer Networks

Page 31: Graphs and Sets Dr. Andrew Wallace PhD BEng(hons) EurIng andrew.wallace@cs.umu.se

Questions?