CSS 343: Notes from Lecture 8 (DRAFT)
Administrivia
-
midterm: next Tuesday
-
Thursday: review
-
Sunday: office hours
-
you may bring single 8.5x11" cheat sheet (double-sided)
-
no calculators
-
assignment 1: due tonight
-
won't close the drop box before some time Thursday AM
-
but we need to move on
-
moving forward today
-
postponing hashing until later in the term
-
need to juggle things a bit so assignment 3 does not get
delayed
-
next up: graphs
-
but first: a couple of things to wrap up with Huffman coding
Frequency/Coding Tables
-
Encoding ASCII: symbol set is 256 elements (8-bit byte)
-
unsigned char is number in the range of 0..255
-
use array of size 256 indexed by
unsigned char
value
-
this is a very concrete implementation of a dictionary
abstraction, optimized for the fact that the table size is fixed
and relatively small.
Bit-at-a-Time Operations
-
bit set/clear/toggle/test: masks with boolean operators:
& (and),
| (or),
^ (xor),
~ (complement)
-
bit shift operators:
<< (left shift)
>> (right shift)
-
bitwise operators are distinct from the corresponding logical
operators:
&& (logical and)
|| (logical or)
! (logical not)
Radix Tree
-
Huffman tree is an example of a binary radix tree (radix: base)
-
a radix tree is a tree where each node represents the next
symbol (digit) in the string (number): lookup time is
O(string-length).
-
this can potentially waste a lot of memory
Graphs
Graphs represent relationsihsp between
things:
problemns that can be described in terms of
-
network
-
circuit
-
web
-
relationship
Examples
-
social network (six degrees of Kevin Bacon)
-
road network (map directions)
-
computer network (packet routing)
-
pipelines (flow network)
Up to now, we've been dealing with structures having a fixed
number of pointers:
-
linked list: single pointer
-
double-linked list: two pointers
-
binary tree: two pointers
-
binary tree with parent pointer: three pointers
-
2-3 tree: three pointers
Graphs have a variable number of pointers.
A tree is a kind of graph.
-
no cycles
-
typically represent hierarchical relationships
Definitions:
-
formal definition: a graph
G = (V, E)
is a set
V
of vertices (nodes)
and a set
E
of edges where an edge
e
is a pair of vertices
(u, v).
-
if the edge pair is ordered, the graph is directed
(digraph);
otherwise the graph is undirected
-
multigraph:
multiple "parallel" edges between some pair of nodes;
otherwise, the graph is
simple.
-
in a simple graph, each node can have at most
N - 1
edges where
N = |V|.
Therefore,
M = |E|
is
O(N2)
-
a graph is
connected
if there is a path (direct or indirect—one or more "hops")
between every pair of nodes.
-
a graph (connected or unconnected)
consists of one or more
connected components
-
complete
graph: edge between every pair of nodes.
-
planar graph: may be drawn on a piece of paper (planar
surface) without any crossing lines
-
planar graphs must be relatively
sparse
-
empty graph:
node(s) but no edges
-
null graph:
no vertices
-
degree (of a vertex):
number of edges
-
directed graph: degree in/degree out
-
undirected graph: the sum of the degree of all vertices is
twice the number of edges
-
G'
is a subgraph of
G
if
G' = (V', E')
where
V'
is a subset of
V
and
E'
is a subset of
E.
-
graph coloring: assigning a color to each node such that no node
is adjacent to another node of the same color
-
this is not the same thing as
decorating
a node with a color
-
2-color graph: bipartite (relatively easy to determine
whether the graph can be colored using only 2 colors)
-
general problem of determining whether a graph is colorable
using at most k colors is
NP-complete
(highly technical term for computationally hard problem)
-
graph representation:
-
class for vertices
-
vertex may contain attributes: label, color, weight, etc.
-
various representations for edges (depending on problem):
-
pointer to vertex (edge has no attributes)
-
std::pair<V*, V*>:
pair
of vertex pointers
class (if edge has attributes, e.g. weight or cost)
edge may be represented by a pointer to vertex (if edge has
no attributes
Graph Problems Galore
-
finding path:
-
depth-first search (DFS)
-
breadth-first search (BFS)
-
least-cost path
-
Dijkstra's algorithm (assigment 3)
-
Travelling Salesman Problem (TSP)
-
NP-complete
-
approximate solutions using heuristics
-
finding connected components
-
finding maximum flow
Shortest-Path Problem
-
unweighted graph: BFS
-
weighted graph: Dijkstra
Depth-First/Breadth-First Search
-
unweighted graph search: may be depth-first (go as deep as you
can, then come back), or breadth-first (go shallow across an
expanding flame front)
Depth-First Search
-
easy recursive algorithm: visit current node calls visit for
each children
-
DFS may be pre-order (process current node before processing
children, on the way down) or post-order (process node after
processing children, on the way back up)
-
may also be implemented non-recursively using explicit stack
Breadth-First Search
-
non-recursive algorithm
-
similar to non-recursive DFS, but uses queue (FIFO) instead of
stack (LIFO)