CS112_42_slides-graphs

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

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

View Full Document Right Arrow Icon
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.
Background image of page 1

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

View Full Document Right Arrow Icon
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.
Background image of page 2
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.
Background image of page 3

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

View Full Document Right Arrow Icon
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
Background image of page 4
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.
Background image of page 5

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

View Full Document Right Arrow Icon
Image of page 6
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 Right Arrow Icon
Ask a homework question - tutors are online