CS112_42_slides-graphs

# CS112_42_slides-graphs - Graphs Yinglin WANG CS Dept SJTU...

This preview shows pages 1–6. Sign up to view the full content.

CS112 Fundamentals of Programming Abstractions Graphs Yinglin WANG CS Dept., SJTU CS112 Fundamentals of Programming Abstractions Bitwise operators An important use of bitwise operators is to combine items from a set of constants, as in this example from a graphics package. Note that the values of the constants are all powers of 2. This means that each constant has a single bit set, and no two are the same.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS112 Fundamentals of Programming Abstractions Bitwise operators We can combine constants, which are attributes of the graph we want to draw, by using the bitwise OR operation. We can later add or remove attributes as needed. DrawBarGraph(myGraph, . ..); myGraph |= AG_BAR_HORIZ; myGraph &= ~AG_BAR_CLUSTER; unsigned int myGraph = AG_BAR_XGRID | AG_BAR_CLUSTER; CS112 Fundamentals of Programming Abstractions Bitwise operators Here is another example from one of the .h files used in programming applications for Microsoft Windows.
CS112 Fundamentals of Programming Abstractions Bitwise operators Just as with the operators = = and =, you have to be very careful to distinguish between && and &. These examples demonstrate that the two operators are similar but not the same. If you meant to write && but wrote & instead, your program would appear to work if you happened to test it with the data shown on the left.If you later happened to encounter the data shown on the right, you would wonder what was going wrong. Similar considerations apply to the operators || and |. && and & are not the same! CS112 Fundamentals of Programming Abstractions Graphs & Our next topic is graphs , which are collections of nodes connected by arcs . The nodes and arcs may contain data of various types.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS112 Fundamentals of Programming Abstractions Undirected Graphs & Consider the route map of airlines in the Figure 16-1 on page 695. & If we assume that flights always operate in both directions, then we use a graph in which every arc has directions of both (or has no specified direction ) . & A graph in which connections run both ways is called a undirected graph. Seattle Boston New York Atlanta Chicago Denver San Francisco Portland Dallas Seattle Boston Chicago Denver San Francisco Portland Dallas CS112 Fundamentals of Programming Abstractions Undirected Graphs & We use the combinations of two sets, the node set V and arc set E , to represent a graph. V ={Atlanta, Boston, Chicago,…} E ={Atlanta Chicago, Atlanta Dallas, Atlanta New York, …} Seattle Boston Chicago Denver San Francisco Portland Dallas
CS112 Fundamentals of Programming Abstractions directed Graphs & In many cases, it makes sense to use directed graphs , in which each arc has a direction. & If your airline operates a plane from San Francisco to Dallas but has the plane stop in Denver on the return flight, that piece of route map looks like as the left.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 23

CS112_42_slides-graphs - Graphs Yinglin WANG CS Dept SJTU...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online