Graphs
Graphs
C++ I/O;
INTRODUCTION TO
GRAPH
Today’s Class
• C++ I/O
• I/O buffering
• Bit-by-bit I/O
• Introduction to Graph
Reading and writing numbers
#include <iostream>
#include <fstream>
Assuming ints are represented with 4 bytes, how large is numfile after this
program runs?
A. 1 byte
B. 4 bytes
C. 5 bytes
D. 20 bytes
Reading and writing numbers
#include <iostream>
#include <fstream>
}
theFile.close();
}
Binary and nonbinary file streams
• Ultimately, all streams are sequences of bytes: input streams, output streams... text
streams, multimedia streams, TCP/IP socket streams...
• However, for some purposes, on some operating systems, text files are handled
differently from binary files
• Line termination characters for a particular platform may be inserted or removed
automatically
• Conversion to or from a Unicode encoding scheme might be performed
• If you don’t want those extra manipulations to occur, use the flag ios::binary when you
open it, to specify that the file stream is a binary stream
• To test your implementation on small strings, use formatted I/O
• But there is one small detail: binary I/O operates on units of information such as
whole byes, or a string of bytes
• We need variable strings of bits
Reading binary data from a file: an example
#include <fstream>
using namespace std;
/** Count and output the number of times char ’a’ occurs in
* a file named by the first command line argument. */
int main(int argc, char** argv) {
ifstream in;
in.open(argv[1], ios::binary);
int count = 0;
unsigned char ch;
while(1) {
ch = in.get(); // or: in.read(&ch,1);
if(! in.good() ) break; // failure, or eof
if(ch == ’a’) count++; // read an ’a’, count it
}
• I/O buffering is the use of an intermediate data structure, called the buffer, usually an
array used with FIFO behavior, to hold data items
• Output buffering: the buffer holds items destined for output until there are enough of
them to send to the destination; then they are sent in one large chunk
• Input buffering: the buffer holds items that have been received from the source in
one large chunk, until the user needs them
• The reason for buffering is that it is often much faster per byte to receive data from a
source, or to send data to a destination, in large chunks, instead of one byte at at time
• This is true, for example, of disk files and internet sockets; even small buffers
(512 or 1K bytes), can make a big difference in performance
• Also, operating system I/O calls and disk drives themselves typically perform buffering
Streams and Buffers
BitOutputStream:
DATA
encoder Buffer ostream
IN bits bytes
• This is standard not only in C++, but in just about every other language in the world
• If you want to do bit-by-bit I/O, you need to write your own methods for it
• For a nice object-oriented design, you can define a class that extends an existing
iostream class, or that delegates to an object of an existing iostream class, and
adds writeBit or readBit methods (and a flush method which flushes the 8-bit buffer)
Today’s Class
• C++ I/O
• I/O buffering
• Bit-by-bit I/O
C++ bitwise operators
• C++ has bitwise logical operators &, |, ^, ~ and shift operators <<, >>
• Operands to these operators can be of any integral type; the type of the result will be the
same as the type of the left operand
<< shifts its left argument left by number of bit positions given by its right
argument, shifting in 0 on the right;
>> shifts its left argument right by number of bit positions given by its right
argument, shifting in the sign bit on the left if the left argument is a signed
type, else shifts in 0
C++ bitwise operators: examples
one byte
unsigned char a = 5, b = 67;
a: 0 0 0 0 0 1 0 1
most least
b: 0 1 0 0 0 0 1 1 significant significant
bit bit
Page 17of 23
Scott B. Baden / CSE 100-A / Spring 2013
C++ bitwise operators: examples
one byte
unsigned char a = 5, b = 67;
a: 0 0 0 0 0 1 0 1
most least
b: 0 1 0 0 0 0 1 1 significant significant
bit bit
Page 18of 23
Scott B. Baden / CSE 100-A / Spring 2013
C++ bitwise operators: examples
one byte
unsigned char a = 5, b = 67;
a: 0 0 0 0 0 1 0 1
most least
b: 0 1 0 0 0 0 1 1 significant significant
bit bit
a & b 0 0 0 0 0 0 0 1
a | b 0 1 0 0 0 1 1 1
~a 1 1 1 1 1 0 1 0
a << 4 0 1 0 1 0 0 0 0
b >> 1 0 0 1 0 0 0 0 1
(b >> 1) & 1 0 0 0 0 0 0 0 1
a | (1 << 5) 0 0 1 0 0 1 0 1
C++ bitwise operators: an exercise
• Selecting a bit: Suppose we want to return the value --- 1 or 0 --- of the nth bit from the
right of a byte argument, and return the result. How to do that?
byte bitVal(char b, int n) {
return
}
• Setting a bit: Suppose we want to set the value --- 1 or 0 --- of the nth bit from the right of
a byte argument, leaving other bits unchanged, and return the result. How to do that?
byte setBit(char b, int bit, int n) {
return
}
Defining classes for bitwise I/O
• For a nice object-oriented design, let’s define a class BitOutputStream that delegates to
an object of an existing iostream class, and that adds a writeBit method (and a flush
method which flushes the 8-bit buffer)
• If instead BitOutputStream subclassed an existing class, it would inherit all the
existing methods of its parent class, and so they become part of the subclass’s interface
also
• some of these methods might be useful, but...
• in general it will complicate the interface
• Otherwise the two design approaches are very similar to implement, except that:
• with inheritance, BitOutputStream uses superclass methods to perform operations
• with delegation, BitOutputStream uses methods of a contained object to perform
operations
char buf
int nbits
ostream out
Outline of a BitInputStream class, using delegation
#include <iostream>
class BitInputStream {
private:
char buf; // one byte buffer of bits
int nbits; // how many bits have been read from buf
std::istream & in; // the input stream to use
public:
}
Sources of information and entropy
• A source of information emits a sequence of symbols drawn independently
from some alphabet
• Suppose the alphabet is the set of symbols ,…,
• Suppose the probability of symbol occurring in the source is
• Then the information contained in symbol is log bits, and the average
information per symbol is (logs are base 2):
• H
What is the best possible average length of a Symbol Frequency
coded symbol with these frequencies? S 1.0
A. 0 P 0.0
B. 0.67 A 0.0
C. 1.0 M 0.0
D. 1.57
E. 2.15
What is the best possible average length of a
coded symbol with this frequency distribution? Symbol Frequency
(why?) S 0.25
A. 1 P 0.25
B. 2 A 0.25
C. 3 M 0.25
D. lg(2)
Graphs
32
Graphs
A. They consist of both vertices and edges
C B. They do NOT have an inherent order
B C. Edges may be weighed or unweighted
D. Edges may be directed or undirected
E E. They may contain cycles
A
D
34
Graphs
Which of the following is true?
C A. A graph can always be represented as a tree
B B. A tree can always be represented as a graph
C. Both A and B
E D. Neither A or B
A
D
35
Graphs
Which of the following is true?
C A. A graph can always be represented as a tree
B B. A tree can always be represented as a graph
C. Both A and B
E D. Neither A or B
A
D
Note that trees are special cases of graphs; lists are special cases of trees.
36
Why Graphs?
C
B
E
A
D
37
Why Graphs?
C
B
E
A
D
Graphs: Example
V0 V1
A directed graph
V2 V3 V4
V5
V={
|V| =
E={
|E|
Path:
39
Graphs: Definitions
V0 V1
A directed graph
V2 V3 V4
V5 V6
• Disconnected graphs:
A. 0, N2
B. N, N2
C. N-1, N(N-1)/2
42
V2 V3 V2 V3
V2 V3 V4
V5 V6
0 1 2 3 4 5 6
A 2D array where each entry [i][j] encodes
0
connectivity information between i and j
1 • For an unweighted graph, the entry is 1
2 if there is an edge from i to j, 0 otherwise
• For a weighted graph, the entry is the
3 weight of the edge from i to j, or “infinity”
4 if there is no edge
5 • Note an undirected graph’s adjacency matrix
will be symmetrical
6
44
V2 V3 V4
V5 V6
V2 V3 V2 V3
0 1 2 3 0 1 2 3
0 0 1 0 0 0 1 1 1 1
1 0 0 0 1 1 1 1 0 1
2 1 0 0 0 2 1 1 0 1
3 0 0 1 0 3 0 1 1 1
V2 V3 V4
V2 V3 V4
V5
Searching a graph
• Find if a path exists between any two nodes
• Find the shortest path between any two nodes
• Find all nodes reachable from a given node
V0 V1
V3 V4
V2
Generic Goals:
• Find everything that can be explored
• Don’t explore anything twice
49
V0 V1
V3 V4
V2
50
V3 V4
V5
V2
51
V3 V4
V5
V2
V3 V4
V5
V2