Liu 的个人资料唯有仰望是真实的照片日志列表更多 ![]() | 帮助 |
|
2006/6/30 view of the msn social relations with h3As you can see in the previous post on this programm, i save the nick->friends_list in a dict. And I find a cool tool to represent the relations. http://graphics.stanford.edu/~munzner/h3/HypView.html
It's in put file format is like this:
INPUT FILE FORMATEach line of the input file is of the form depth identifier 1 group1 [group2 ... groupN] with the following types int string int string [string ... string] Each line corresponds to a node. The order of the lines is meaningful. The integer at the beginning of the line is the depth in the tree. A line with depth one greater than the line above it indicates a link between two nodes from the line above to the line below. The identifier strings are assumed to uniquely specify a node. If an identifier occurs twice that means a node has more than one incoming link, so the file describes a general graph, as opposed to a tree or a directed acyclic graph (DAG). The identifier string is used as an interface between the library and the application program in many methods which have the argument "string & id". A file with structure like 0 A [...] 1 B [...] 2 C [...] 3 D [...] 4 A [...] 3 E [...] 2 F [...] 1 G [...] 1 H [...] 2 I [...] 2 J [...] 1 K [...] corresponds to a graph that looks like: .-------- A / ____|_______ | / | \ | | B G H K | / \ / \ | C F I J \ / \ D E The very first node is the root, the numbers correspond to the number of layers deep in the hierarchy.
And this is the code i use to build the input file from my dict. import cPickle def unescape(s): def get_all(d): try: if degree%2: # some quick hack to make the image colorful, I don't know what the magic number means:P global degree_d if __name__ == "__main__": And to give you a screenshot of the picture.
www.doovee.comCS多媒体组做的似乎是, 蛮好的, 看了那个雨一直下的搞笑版, 里面那个mm好pp的说, 哈哈 Madnesswrited by Jp Calderone on March 5th, 2004
Twisted "chat server" in one expression Tonight's messed up code: 2006/6/29 twisted version of the msn space social graph buildAfter my post on the c.p.t, I received some help from a kind guy, but he write to me privately, so i'm not going to put the messege here. So below is the twisted version of the code, very ugly, but works. I want to get the argument passing right later.
And also, if you know some way to draw a directed graph nicely, please let me know.
import re, sys, socket
import urllib from twisted.internet import reactor
from twisted.python import log from twisted.web import client from sets import Set socket.setdefaulttimeout = 10 all_buddies = Set() def get_buddy(page):#, buddy=None): def multi_buddy_factory(buddies): for buddy in next_to_visit: url = " http://"+buddy+".spaces.msn.com/" try: got(url)
except: continue def got(url):#, buddy): log.startLogging(sys.stdout) URL = "http://ayueer.spaces.msn.com/" got(URL)#, "ayueer")
try: reactor.run() except KeyboardInterrupt: print all_buddies 2006/6/28 today's /. articleSomething Bruce Schneier reporting on his blog about the recent paper "how to defeat China's national firewall" .
on Bruce Schneier's site http://www.schneier.com/blog/archives/2006/06/ignoring_the_gr.html
the cambridge post http://www.cl.cam.ac.uk/users/sjm217/volatile/cache/2006/06/27/ignoring-the-great-firewall-of-china/
No comments. my post on comp.python.twistedFrom: Jia Liu <ayuer.python <at> gmail.com> Subject: Naive questions about how to do it twisted way. Newsgroups: gmane.comp.python.twisted Date: 2006-06-28 09:07:14 GMT (1 hour and 56 minutes ago)
Hi,
I have been follow twisted for like half a year now, I have read the doc and the oreilly book, but i still cann't do some simple work in the twisted way. Could someone give me a hint?
Like today, i write this little code to follow my friends list on my msn space, and get their friends list, so on and so on, and maybe at last I can build a social relationship graph with it. The code is actually really navie, here it goes. Sorry for the ugliness of my code, please help me make it prettier:)
Thanks. Appologize for my poor English.
Jia Liu
import re, sys, socket socket.setdefaulttimeout = 10 all_buddies = Set() def get_buddy(page, buddy=None): def got(url, buddy=None): buddies_of_this_page = get_buddy(page, buddy) try: -- 银筝夜久殷勤弄,心怯空房不忍归 2006/6/27 python unicode browser(zz)interesting:)
Python unicode browser
In the process of playing around with punycode today, I ended up writing this: Make sure your terminal supports UTF-8 or you will not experience the full enjoyment. socket.recv -- three ways to turn it into recvinto(zz)Ways to reduce the overheat of malloc() in socket.recv(), looks good, maybe some day when I have time to get up my lazy ass and take some measurements on performence.
socket.recv -- three ways to turn it into recvinto Inspired by http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/408859 While some people are busy worrying about how to make Python's builtin sockets less efficient, one might be wondering if the reverse is possible - how do you make them more efficient? After all, you usally want your program to run more quickly, or tax your CPU less heavily, or consume fewer resources, not the reverse. Fortunately, I have just the solution for you1. The approach explored below will be to avoid allocating new memory when reading from the socket. Since malloc() is a relatively expensive operation, this will save us a bunch of CPU time, as well as saving us memory by reducing chances for heap fragmentation and so forth.
So there you have it. Happy networking. 1 Sorry, it's way too late to post something useful. Especially when I could post something fun instead. 2 Note: in each example where socket IO occurs, I have launched telnet in another terminal and type in some random bytes. keep on stealling from Jp CalderoneIt seems there's a problem with tee when the file is too big, so do it by yourself.
Doubling bytestreams For a while now, every day, we have been moving a somewhat hefty chunk of bytes, about 15GB worth from one machine onto another. The bytes are a tar file, generated in realtime and piped to ssh (connected to a host untaring the bytes). Pretty standard stuff, really. A while back we decided we wanted to send this tar to two hosts, instead of one. No big deal, we just ran tar twice, piping the output to an ssh process connected to a different host each time. Worked like a charm. Recently, we decided the load incurred by the second copy was heavy enough to be worth avoiding. Obvious solution: pipe tar to tee, send one of tee's outputs to one ssh, the other to the other. That doesn't work. Woops. For whatever reason, tee chokes after a bit less than 8 GB of data.
I called it yjoint, uncreative clod that I am (Hey, at least I didn't call it pytee). It's not exactly a drop-in tee replacement. We use it more or less like this: exarkun@boson:~/$ tar c yummy_data | yjoint \ > --out >(ssh host1 tar x) \ > --out >(ssh host2 tar x) Swapped tee out and yjoint in, and suddenly we are in business again. I wonder what the deal with tee is? what really happened on Mars(zz)呵呵, 讲到有趣的bug, 我就想到这个了
下面是在http://research.microsoft.com/~mbj/这位同学主页上的两篇文章.
From: Mike Jones <mbj@microsoft.com> The Mars Pathfinder mission was widely proclaimed as "flawless" in the early days after its July 4th, 1997 landing on the Martian surface. Successes included its unconventional "landing" -- bouncing onto the Martian surface surrounded by airbags, deploying the Sojourner rover, and gathering and transmitting voluminous data back to Earth, including the panoramic pictures that were such a hit on the Web. But a few days into the mission, not long after Pathfinder started gathering meteorological data, the spacecraft began experiencing total system resets, each resulting in losses of data. The press reported these failures in terms such as "software glitches" and "the computer was trying to do too many things at once". This week at the IEEE Real-Time Systems Symposium I heard a fascinating keynote address by David Wilner, Chief Technical Officer of Wind River Systems. Wind River makes VxWorks, the real-time embedded systems kernel that was used in the Mars Pathfinder mission. In his talk, he explained in detail the actual software problems that caused the total system resets of the Pathfinder spacecraft, how they were diagnosed, and how they were solved. I wanted to share his story with each of you. VxWorks provides preemptive priority scheduling of threads. Tasks on the Pathfinder spacecraft were executed as threads with priorities that were assigned in the usual manner reflecting the relative urgency of these tasks. Pathfinder contained an "information bus", which you can think of as a shared memory area used for passing information between different components of the spacecraft. A bus management task ran frequently with high priority to move certain kinds of data in and out of the information bus. Access to the bus was synchronized with mutual exclusion locks (mutexes). The meteorological data gathering task ran as an infrequent, low priority thread, and used the information bus to publish its data. When publishing its data, it would acquire a mutex, do writes to the bus, and release the mutex. If an interrupt caused the information bus thread to be scheduled while this mutex was held, and if the information bus thread then attempted to acquire this same mutex in order to retrieve published data, this would cause it to block on the mutex, waiting until the meteorological thread released the mutex before it could continue. The spacecraft also contained a communications task that ran with medium priority. Most of the time this combination worked fine. However, very infrequently it was possible for an interrupt to occur that caused the (medium priority) communications task to be scheduled during the short interval while the (high priority) information bus thread was blocked waiting for the (low priority) meteorological data thread. In this case, the long-running communications task, having higher priority than the meteorological task, would prevent it from running, consequently preventing the blocked information bus task from running. After some time had passed, a watchdog timer would go off, notice that the data bus task had not been executed for some time, conclude that something had gone drastically wrong, and initiate a total system reset. This scenario is a classic case of priority inversion. HOW WAS THIS DEBUGGED?VxWorks can be run in a mode where it records a total trace of all interesting system events, including context switches, uses of synchronization objects, and interrupts. After the failure, JPL engineers spent hours and hours running the system on the exact spacecraft replica in their lab with tracing turned on, attempting to replicate the precise conditions under which they believed that the reset occurred. Early in the morning, after all but one engineer had gone home, the engineer finally reproduced a system reset on the replica. Analysis of the trace revealed the priority inversion. HOW WAS THE PROBLEM CORRECTED?When created, a VxWorks mutex object accepts a boolean parameter that indicates whether priority inheritance should be performed by the mutex. The mutex in question had been initialized with the parameter off; had it been on, the low-priority meteorological thread would have inherited the priority of the high-priority data bus thread blocked on it while it held the mutex, causing it be scheduled with higher priority than the medium-priority communications task, thus preventing the priority inversion. Once diagnosed, it was clear to the JPL engineers that using priority inheritance would prevent the resets they were seeing. VxWorks contains a C language interpreter intended to allow developers to type in C expressions and functions to be executed on the fly during system debugging. The JPL engineers fortuitously decided to launch the spacecraft with this feature still enabled. By coding convention, the initialization parameter for the mutex in question (and those for two others which could have caused the same problem) were stored in global variables, whose addresses were in symbol tables also included in the launch software, and available to the C interpreter. A short C program was uploaded to the spacecraft, which when interpreted, changed the values of these variables from FALSE to TRUE. No more system resets occurred. ANALYSIS AND LESSONSFirst and foremost, diagnosing this problem as a black box would have been impossible. Only detailed traces of actual system behavior enabled the faulty execution sequence to be captured and identified. Secondly, leaving the "debugging" facilities in the system saved the day. Without the ability to modify the system in the field, the problem could not have been corrected. Finally, the engineer's initial analysis that "the data bus task executes very frequently and is time-critical -- we shouldn't spend the extra time in it to perform priority inheritance" was exactly wrong. It is precisely in such time critical and important situations where correctness is essential, even at some additional performance cost. HUMAN NATURE, DEADLINE PRESSURESDavid told us that the JPL engineers later confessed that one or two system resets had occurred in their months of pre-flight testing. They had never been reproducible or explainable, and so the engineers, in a very human-nature response of denial, decided that they probably weren't important, using the rationale "it was probably caused by a hardware glitch". Part of it too was the engineers' focus. They were extremely focused on ensuring the quality and flawless operation of the landing software. Should it have failed, the mission would have been lost. It is entirely understandable for the engineers to discount occasional glitches in the less-critical land-mission software, particularly given that a spacecraft reset was a viable recovery strategy at that phase of the mission. THE IMPORTANCE OF GOOD THEORY/ALGORITHMSDavid also said that some of the real heroes of the situation were some people from CMU who had published a paper he'd heard presented many years ago who first identified the priority inversion problem and proposed the solution. He apologized for not remembering the precise details of the paper or who wrote it. Bringing things full circle, it turns out that the three authors of this result were all in the room, and at the end of the talk were encouraged by the program chair to stand and be acknowledged. They were Lui Sha, John Lehoczky, and Raj Rajkumar. When was the last time you saw a room of people cheer a group of computer science theorists for their significant practical contribution to advancing human knowledge? :-) It was quite a moment. POSTLUDEFor the record, the paper was: L. Sha, R. Rajkumar, and J. P. Lehoczky. Priority Inheritance Protocols: An Approach to Real-Time Synchronization. In IEEE Transactions on Computers, vol. 39, pp. 1175-1185, Sep. 1990. This note was widely circulated after I sent it to a few friends in the systems community. Among other places, it appeared in Peter G. Neumann's moderated Risks Forum (comp.risks) on Tuesday, 9 December 1997 in issue RISKS-19.49. Be sure to also read the follow-up message from Glenn Reeves of JPL, who led the software team for the Mars Pathfinder spacecraft. Related Links:
From: Glenn E Reeves[SMTP:Glenn.E.Reeves@jpl.nasa.gov] What really happened on Mars ?By now most of you have read Mike’s (mbj@microsoft.com) summary of Dave Wilner’s comments given at the IEEE Real-Time Systems Symposium. I don’t know Mike and I didn’t attend the symposium (though I really wish I had now) and I have not talked to Dave Wilner since before the talk. However, I did lead the software team for the Mars Pathfinder spacecraft. So, instead of trying to find out what was said I will just tell you what happened. You can make your own judgments. I sent this message out to everyone who was a recipient of Mike’s original that I had an email address for. Please pass it on to anyone you sent the first one to. Mike, I hope you will post this wherever you posted the original. Since I want to make sure the problem is clearly understood I need to step through each of the areas which contributed to the problem. THE HARDWAREThe simplified view of the Mars Pathfinder hardware architecture looks like this. A single CPU controls the spacecraft. It resides on a VME bus which also contains interface cards for the radio, the camera, and an interface to a 1553 bus. The 1553 bus connects to two places : The "cruise stage" part of the spacecraft and the "lander" part of the spacecraft. The hardware on the cruise part of the spacecraft controls thrusters, valves, a sun sensor, and a star scanner. The hardware on the lander part provides an interface to accelerometers, a radar altimeter, and an instrument for meteorological science known as the ASI/MET. The hardware which we used to interface to the 1553 bus (at both ends) was inherited from the Cassini spacecraft. This hardware came with a specific paradigm for its usage : the software will schedule activity at an 8 Hz rate. This **feature** dictated the architecture of the software which controls both the 1553 bus and the devices attached to it. THE SOFTWARE ARCHITECTUREThe software to control the 1553 bus and the attached instruments was implemented as two tasks. The first task controlled the setup of transactions on the 1553 bus (called the bus scheduler or bc_sched task) and the second task handled the collection of the transaction results i.e. the data. The second task is referred to as the bc_dist (for distribution) task. A typical timeline for the bus activity for a single cycle is shown below. It is not to scale. This cycle was constantly repeated. |< ------------- .125 seconds ------------------------>| |<***************| |********| |**>| |<- bc_dist active ->| bc_sched active
|< -bus active ->| |<->|----|----------------|--------------------|--------|---|---|-------
t1 t2 t3 t4 t5 t1
The *** are periods when tasks other than the ones listed are executing. Yes, there is some idle time. t1 - bus hardware starts via hardware control on the 8 Hz boundary. The transactions for the this cycle had been set up by the previous execution of the bc_sched task. The bc_sched and bc_dist tasks check each cycle to be sure that the other had completed its execution. The bc_sched task is the highest priority task in the system (except for the vxWorks "tExec" task). The bc_dist is third highest (a task controlling the entry and landing is second). All of the tasks which perform other spacecraft functions are lower. Science functions, such as imaging, image compression, and the ASI/MET task are still lower. Data is collected from devices connected to the 1553 bus only when they are powered. Most of the tasks in the system that access the information collected over the 1553 do so via a double buffered shared memory mechanism into which the bc_dist task places the latest data. The exception to this is the ASI/MET task which is delivered its information via an interprocess communication mechanism (IPC). The IPC mechanism uses the vxWorks pipe() facility. Tasks wait on one or more IPC "queues" for messages to arrive. Tasks use the select() mechanism to wait for message arrival. Multiple queues are used when both high and lower priority messages are required. Most of the IPC traffic in the system is not for the delivery of real-time data. However, again, the exception to this is the use of the IPC mechanism with the ASI/MET task. The cause of the reset on Mars was in the use and configuration of the IPC mechanism. THE FAILUREThe failure was identified by the spacecraft as a failure of the bc_dist task to complete its execution before the bc_sched task started. The reaction to this by the spacecraft was to reset the computer. This reset reinitializes all of the hardware and software. It also terminates the execution of the current ground commanded activities. No science or engineering data is lost that has already been collected (the data in RAM is recovered so long as power is not lost). However, the remainder of the activities for that day were not accomplished until the next day. The failure turned out to be a case of priority inversion (how we discovered this and how we fixed it are covered later). The higher priority bc_dist task was blocked by the much lower priority ASI/MET task that was holding a shared resource. The ASI/MET task had acquired this resource and then been preempted by several of the medium priority tasks. When the bc_sched task was activated, to setup the transactions for the next 1553 bus cycle, it detected that the bc_dist task had not completed its execution. The resource that caused this problem was a mutual exclusion semaphore used within the select() mechanism to control access to the list of file descriptors that the select() mechanism was to wait on. The select mechanism creates a mutual exclusion semaphore to protect the "wait list" of file descriptors for those devices which support select. The vxWorks pipe() mechanism is such a device and the IPC mechanism we used is based on using pipes. The ASI/MET task had called select, which had called pipeIoctl(), which had called selNodeAdd(), which was in the process of giving the mutex semaphore. The ASI/ MET task was preempted and semGive() was not completed. Several medium priority tasks ran until the bc_dist task was activated. The bc_dist task attempted to send the newest ASI/MET data via the IPC mechanism which called pipeWrite(). pipeWrite() blocked, taking the mutex semaphore. More of the medium priority tasks ran, still not allowing the ASI/MET task to run, until the bc_sched task was awakened. At that point, the bc_sched task determined that the bc_dist task had not completed its cycle (a hard deadline in the system) and declared the error that initiated the reset. HOW WE FOUND ITThe software that flies on Mars Pathfinder has several debug features within it that are used in the lab but are not used on the flight spacecraft (not used because some of them produce more information than we can send back to Earth). These features were not "fortuitously" left enabled but remain in the software by design. We strongly believe in the "test what you fly and fly what you test" philosophy. One of these tools is a trace/log facility which was originally developed to find a bug in an early version of the vxWorks port (Wind River ported vxWorks to the RS6000 processor for us for this mission). This trace/log facility was built by David Cummings who was one of the software engineers on the task. Lisa Stanley, of Wind River, took this facility and instrumented the pipe services, msgQ services, interrupt handling, select services, and the tExec task. The facility initializes at startup and continues to collect data (in ring buffers) until told to stop. The facility produces a voluminous dump of information when asked. After the problem occurred on Mars we did run the same set of activities over and over again in the lab. The bc_sched was already coded so as to stop the trace/log collection and dump the data (even though we knew we could not get the dump in flight) for this error. So, when we went into the lab to test it we did not have to change the software. In less that 18 hours we were able to cause the problem to occur. Once we were able to reproduce the failure the priority inversion problem was obvious. HOW WAS THE PROBLEM CORRECTEDOnce we understood the problem the fix appeared obvious : change the creation flags for the semaphore so as to enable the priority inheritance. The Wind River folks, for many of their services, supply global configuration variables for parameters such as the "options" parameter for the semMCreate used by the select service (although this is not documented and those who do not have vxWorks source code or have not studied the source code might be unaware of this feature). However, the fix is not so obvious for several reasons : 1) The code for this is in the selectLib() and is common for all device creations. When you change this global variable all of the select semaphores created after that point will be created with the new options. There was no easy way in our initialization logic to only modify the semaphore associated with the pipe used for bc_dist task to ASI/MET task communications. We did end up modifying the global variable to include the priority inversion. This corrected the problem. We asked Wind River to analyze the potential impacts for (3) and (4). They concluded that the performance impact would be minimal and that the behavior of select() would not change so long as there was always only one task waiting for any particular file descriptor. This is true in our system. I believe that the debate at Wind River still continues on whether the priority inversion option should be on as the default. For (1) and (2) the change did alter the characteristics of all of the select semaphores. We concluded, both by analysis and test, that there was no adverse behavior. We tested the system extensively before we changed the software on the spacecraft. HOW WE CHANGED THE SOFTWARE ON THE SPACECRAFTNo, we did not use the vxWorks shell to change the software (although the shell is usable on the spacecraft). The process of "patching" the software on the spacecraft is a specialized process. It involves sending the differences between what you have onboard and what you want (and have on Earth) to the spacecraft. Custom software on the spacecraft (with a whole bunch of validation) modifies the onboard copy. If you want more info you can send me email. WHY DIDN’T WE CATCH IT BEFORE LAUNCH ?The problem would only manifest itself when ASI/MET data was being collected and intermediate tasks were heavily loaded. Our before launch testing was limited to the "best case" high data rates and science activities. The fact that data rates from the surface were higher than anticipated and the amount of science activities proportionally greater served to aggravate the problem. We did not expect nor test the "better than we could have ever imagined" case. HUMAN NATURE, DEADLINE PRESSURESWe did see the problem before landing but could not get it to repeat when we tried to track it down. It was not forgotten nor was it deemed unimportant. Yes, we were concentrating heavily on the entry and landing software. Yes, we considered this problem lower priority. Yes, we would have liked to have everything perfect before landing. However, I don’t see any problem here other than we ran out of time to get the lower priority issues completed. We did have one other thing on our side; we knew how robust our system was because that is the way we designed it. We knew that if this problem occurred we would reset. We built in mechanisms to recover the current activity so that there would be no interruptions in the science data (although this wasn’t used until later in the landed mission). We built in the ability (and tested it) to go through multiple resets while we were going through the Martian atmosphere. We designed the software to recover from radiation induced errors in the memory or the processor. The spacecraft would have even done a 60 day mission on its own, including deploying the rover, if the radio receiver had broken when we landed. There are a large number of safeguards in the system to ensure robust, continued operation in the event of a failure of this type. These safeguards allowed us to designate problems of this nature as lower priority. We had our priorities right. ANALYSIS AND LESSONSDid we (the JPL team) make an error in assuming how the select/pipe mechanism would work ? Yes, probably. But there was no conscious decision to not have the priority inversion enabled. We just missed it. There are several other places in the flight software where similar protection is required for critical data structures and the semaphores do have priority inversion protection. A good lesson when you fly COTS stuff - make sure you know how it works. Mike is quite correct in saying that we could not have figured this out **ever** if we did not have the tools to give us the insight. We built many of the tools into the software for exactly this type of problem. We always planned to leave them in. In fact, the shell (and the stdout stream) were very useful the entire mission. If you want more detail send me a note. SETTING THE RECORD STRAIGHTFirst, I want to make sure that everyone understands how I feel in regard to Wind River. These folks did a fantastic job for us. They were enthusiastic and supported us when we came to them and asked them to do an affordable port of vxWorks. They delivered the alpha version in 3 months. When we had a problem they put some of the brightest engineers I have ever worked with on the problem. Our communication with them was fantastic. If they had not done such a professional job the Mars Pathfinder mission would not have been the success that it is. Second, Dave Wilner did talk to me about this problem before he gave his talk. I could not find my notes where I had detailed the description of the problem. So, I winged it and I sure did get it wrong. Sorry Dave. ACKNOWLEDGMENTSFirst, thanks to Mike for writing a very nice description of the talk. I think I have had probably 400 people send me copies. You gave me the push to write the part of the Mars Pathfinder End-of-Mission report that I had been procrastinating doing. A special thanks to Steve Stolper for helping me do this. The biggest thanks should go to the software team that I had the privilege of leading and whose expertise allowed us to succeed :
Also ,
--------------------- Glenn Reeves Related Links: 转一个比较ft的bug还是从http://jcalderone.livejournal.com/看到的, 想到了那天在调那个pie程序的时候, 怎么也找不到为什么不能用C-g然后7选特殊模式, 好只选有附件的帖子, 结果最后才发现原来pie跑得是匿名, 匿名在水母那个C-g然后7什么也没有, 晕死...
How Not To Make A Page-Atomic Copy Of A File
Can you find the hidden bug?
在听有里知花的歌, 呵呵从bigjoe大哥那里知道这个名字, 听了那个I cry满好听的. All about hanoi昨天帮一个小朋友写数据结构作业, 一个用栈来做非递归hanoi, 算法是小朋友从书上找到的, 只用实现就好了, 然后在我数据结构课本上看到了递归的实现, 下面两个是抄来的.
void move(int count, int start, int finish, int temp) //recursive
{ if(count > 0){ move(count - 1, start, temp, finish); cout <<"Move disk "<<count<<" from " << start <<" to "<<finish<<"."<<endl; move(count - 1, temp, finish, start); } } void move(int count, int start, int finish, int temp) //without tail recursion
{ int swap; while(count > 0){ move(count - 1, start, temp, finish); cout << "Move disk "<<count<<" from "<<start <<" to "<<finish<<"."<<endl; count--; swap = start; start = temp; temp = swap; } } 然后是我写的栈来做的, 开始的时候做的有些笨, 完全照着算法翻译, 用count到一来判断是否cout, 即中间真正移动的那一步, 然后就没有了每次是移动的哪个盘子的信息. 今天同事帮着想到是怎么回事的, 555, 好zt. typedef struct
{ int count; bool exchange; int start; int finish; int swap; } StepType; void move(int count, int start, int finish, int swap)
{ stack<StepType> S; StepType temp1, temp2; temp1.count = count;
temp1.exchange = false; temp1.start = start; temp1.finish = finish; temp1.swap = swap; S.push(temp1); while(!S.empty()) { temp2 = S.top(); S.pop(); if(temp2.exchange){ cout << "Move disk "<<temp2.count<<" from "<<temp2.start <<" to "<<temp2.finish<<"."<<endl; } else if(temp2.count > 0) { temp1.count = temp2.count - 1; temp1.start = temp2.swap; temp1.swap = temp2.start; temp1.finish = temp2.finish; temp1.exchange = false; S.push(temp1); temp1.exchange = true; temp1.count = temp2.count; temp1.start = temp2.start; temp1.swap = temp2.swap; temp1.finish = temp2.finish; S.push(temp1); temp1.count = temp2.count - 1; temp1.start = temp2.start; temp1.swap = temp2.finish; temp1.finish = temp2.swap; temp1.exchange = false; S.push(temp1); } } } 然后下午就看到水母programming版讨论非递归(不用栈)来做的方法, 因为昨天就在topcoder上看到大家都是从"Algorithms in C++" by Sedgewick上看到的, 因为我没有这本书, 所以只能从他们转述的来理解,不过还是很有趣的算法.
topcoder上的小朋友如是说
In short - if you have n slices to move in 2^n - 1 moves, you enumerate steps by numbers from 1 to (2^n - 1) inclusive, and enumerate all slices with number from 0 to (n-1), inclusive.
On the step i, you calculate the maximal k, that (i % (2^k)) == 0, and move the slice number k (you can prove that moving that slice will be legal at that moment). I don't remember how do you determine where to move it, but this is obvious for all slices except the smallest one, and for the smallest one there is a simple algo. One other way of presenting the non-recursive algorithm:
- on odd moves, move the smallest disc one peg to the right (or from the rightmost to the leftmost peg) - on even moves, make the only move not involving the smallest peg. 水母上是这样说的
n个盘子,都放在A,要经过C放到B
按照从小到大的顺序编号为1、2、……、n 1个盘子
1 把1挪到B 2个盘子
01 把1挪到C 10 把2挪到B 11 把1挪到B 3个盘子
001 把1挪到B 010 把2挪到C 011 把1挪到C 100 把3挪到B 101 把1挪到A 110 把2挪到B 111 把1挪到B 很显然,循环变量从1到2^n - 1循环,每次要挪动的盘子编号对应于循环变量从低到高
第一个为1的比特 或者考虑一颗满二叉树的中序遍历过程,一共有2^n - 1个节点,每步遍历到哪层也是由
这个循环变量的最低的非0比特位确定的 挺有趣的, 什么时候找到书了看一下
贴一下他们给的代码, mathli的c代码, 没看懂, 放着慢慢看
#include <stdio.h>
void hanoi(int n) { int i, j, f, a=1, b = (n%2)?3:2; if (n<1 || n>30) return; printf("%d from %c to %c\n", 1, 'A'-1+a, 'A'-1+b); for (n=(1<<n)-1, i=1; i<n; i+=2) { for (f=1,j=i; j%2; j/=2,f++); if (f%2) a^=b; b^=a; printf("%d from %c to %c\n", f, 'A'-1+a, 'A'-1+b); a^=b; if (f%2) b^=a; printf("%d from %c to %c\n", 1, 'A'-1+a, 'A'-1+b); } } int main(int argc, char *argv[]) { int n; scanf("%d", &n); hanoi(n); return 0; } asterix的python代码, 这个写的还比较好懂
#!/usr/bin/python
""" inorder traversal the complete binary-tree. index is the index of the coresponed heap of the tree. i, j, k stands for the peg 1, 2, 3. """ def left((index, i, j)): #index references the binary heap. 1-based. return (2*index, i, 6-i-j) def right((index, i, j)):
return (2*index+1, 6-i-j, j) def parent((index, i, j)):
k = 6-i-j if index%2: # right child of parent return ((index-1)/2, k, j) else: # left return (index/2, i, k) def first(plates, (index, i, j)): #get leftmost node of subtree
while 2*index <2 **plates: (index, i, j) = left((index, i, j)) return (index, i, j) def next(plates, (index, i, j)): #next node in in-order traversal
if 2*index < 2**plates: # not a leaf return first(plates, right((index, i, j))) elif index % 2: # right leaf while (index != 1) and (index % 2): #go up till reaches a left child (index, i, j) = parent((index, i, j)) if index == 1: # reached root return None # is last node else: return parent((index, i, j)) else: # left leaf return parent((index, i, j)) def iterativeHanoi(plates, i, j):
node = first(plates, (1, i, j)) while node: print node[1], "->", node[2] node = next(plates, node) iterativeHanoi(8, 1, 2) #test case, I added this
------------------------------------------
xiaxia's summary on smth
发信人: xiaxia (心态好才是真的好), 信区: Programming
标 题: Re: the towers of hanoi 发信站: 水木社区 (Wed Jun 28 15:05:52 2006), 站内 狗尾续貂下 整理了下 汉诺塔问题介绍: 在印度,有这么一个古老的传说:在世界中心贝拿勒斯(在印度北部)的圣庙里,一块黄铜板上插着三根宝石针。印度教的主神梵天在创造世界的时候,在其中一根针上从下到上地穿好了由大到小的64片金片,这就是所谓的汉诺塔。不论白天黑夜,总有一个僧侣在按照下面的法则移动这些金片,一次只移动一片,不管在哪根针上,小片必在大片上面。当所有的金片都从梵天穿好的那根针上移到另外一概针上时,世界就将在一声霹雳中消灭,梵塔、庙宇和众生都将同归于尽。 递归算法: 定义 void Hanoi(char src, char des, char via, int n) 表示把n个盘子从src上借助via移动到des上。 显然有 void Hanoi(char src, char des, char via, int n) { Hanoi(src, via, des, n - 1); Move(src, des, n); //把第n个盘子直接从src移动到des Hanoi(via,des, src, n - 1); } 根据递归算法,设f(n)为n个盘子要移动的次数。 那么显然 f(n + 1) = 2*f(n) + 1 -> [f(n + 1) + 1] = 2*[f(n) + 1] f(1) = 1,-> f(n) + 1 = (1 + 1)^n -> f(n) = 2^n - 1。 f(64)= 2^64-1=18446744073709551615 非递归算法: 定义从小到大的盘子序号分别为1,2,……n。 可以用一个1到2^n - 1的2进制序列可以模拟出n个盘子的汉诺塔过程中被移动的盘子的序号序列。即给定一个n,我们通过0到2^n - 1序列可以判断出任意一步应该移动那个盘子。 判断方法:第m步移动的盘子序号是m用二进制表示的最低位bit为1的位置。 证明: n = 1,显然成立。 假设n = k 成立。 n = k + 1时,对应序列1到2^(k+1) - 1,显然这个序列关于2^k左右对称。 假设我们要把k + 1个盘子从A移动C。 那么2^k可以对应着Move(k + 1, A, C)。 1 到 2^k - 1 根据假设可以 对应Hanoi(A, B, C, k)。至于2^k + 1 到 2^(k + 1) - 1把最高位的1去掉对应序列 变成1到2^k - 1,显然2^k + 1 到 2^(k + 1) - 1和1到2^k - 1这两个序列中的对应元素的最低位bit为1的位置相同。 因此2^k + 1 到 2^(k + 1) - 1可以对应Hanoi(B, C,A,k)。 所以对n = k + 1也成立。 下面讨论第m步应该移动对应的盘子从那到那? 定义顺序为 A->B->C->A, 逆序为C->B->A->C。 性质对n个盘子的汉诺塔,任意一个盘子k(k <= n)k在整个汉诺塔的移动过程中要么一直顺序的,要么一直逆序的。而且如果k在n个盘子移动过程的顺序和k - 1(如果k > 1)以及k + 1(如果k < n)的顺序是反序。 比如:n = 3 1 A->C 2 A->B 1 C->B 3 A->C 1 B->A 2 B->C 1 A->C 其中1的轨迹A->C->B->A>C逆序,2的轨迹A->B->C顺序,3的轨迹A->C逆序 证明:假设n <= k成立 对于n = k + 1 根据递归算法 Hanoi(A,C,B,k + 1) = Hanoi(A, B, C, k) + Move(A, C, k + 1) + Hanoi(B, C,A,k); 整个过程中盘子k + 1只移动一次A->C为逆序对应着2^k。 对于任意盘子m < k + 1, m盘子的移动由两部分组成一部分是前半部分Hanoi(A, B, C, k)以及后半部分的Hanoi(B, C,A,k)组成。显然有如果m在Hanoi(A, C, B, k)轨迹顺序的话,则m在Hanoi(A, B, C, k)以及Hanoi(B, C,A,k)都是逆序。 反之亦然。这两部分衔接起来就会证明m在Hanoi(A,C,B,k)和Hanoi(A,C,B,k + 1)中是反序的。同时有Hanoi塔中最大的盘子永远是逆序且只移动1步,A->C。 这样的话:m = k + 1,在Hanoi(A,C,B,k + 1)中是逆序。 m = k,由于在Hanoi(A,C,B,k)中是逆序的,所以Hanoi(A,C,B,k + 1)中是顺序的。 m = k - 1,由于在Hanoi(A,C,B,k - 1)是逆序的,所以Hanoi(A,C,B,k)是顺序的,所以Hanoi(A,C,B,k + 1)是逆序的。 依次下去…… 结论得证。 总结:在n个汉诺中n, n - 2, n - 4……是逆序移动,n - 1, n - 3,n - 5……是顺序移动。 有了以上结论,非递归的程序就很好写了。写了个递归和非递归比较: #include <iostream> using namespace std; void Hanoi(char src, char des, char via, int n) { if(n == 1) { cout << n <<" : "<< src <<" --> " <<des << endl; return; } Hanoi(src, via, des, n - 1); cout << n <<" : "<< src <<" --> " <<des << endl; Hanoi(via, des, src, n - 1); } int main() { int n; cin >> n; cout<<"recusive:"<< endl; Hanoi('A','C','B', n); cout << endl; cout<<"normal:"<<endl; char order[2][256]; char pos[64]; order[0]['A'] = 'B'; order[0]['B'] = 'C'; order[0]['C'] = 'A'; order[1]['A'] = 'C'; order[1]['B'] = 'A'; order[1]['C'] = 'B'; //0是顺序 1是逆序 int index[64]; //确定轨迹的顺序还是逆序 int i, j, m; for(i = n; i > 0; i -= 2) index[i] = 1; for(i = n - 1; i > 0; i -= 2) index[i] = 0; memset(pos, 'A', sizeof(pos)); for(i = 1; i < 1 << n; i ++) { for(m = 1, j = i; j%2 == 0; j/=2, m ++); cout << m <<" : "<< pos[m] <<" --> " << order[index[m]][pos[m]] << endl; pos[m] = order[index[m]][pos[m]]; } return 0; } 最后, 有人贴了一个算法导论上的习题, 想了很久想不出, 先放着, 慢慢想
Give a nonrecursive algorithm that performs an inorder tree walk.
(Hint: There is an easy solution that uses a stack as an auxiliary data structure and a more complicated but elegant solution that uses n o stack but assumes that two pointers can be tested for equality.) the stack one from gamedev http://www.gamedev.net/community/forums/topic.asp?topic_id=33282
struct Node
{ Node* left; Node* right; T data; }; void WalkInorder( Node* root ) { Node* stack[]; //allocate this however you want. Node* iter = stack; *iter++ = 0; Node* curr = root; while( curr ) { while( curr ) { *iter++ = curr; curr = curr->left; } if( *--iter ) { ProcessNode( *iter ); curr = *iter->right; } } }; Simple Twisted Application for Fun <strikethrough>and Profit</strikethrough>(zz)Twisted is really fun, and the people behind it, what can i say, they are crazy( in the nice way, haha:)
Maybe later i will post my little program used to grab the rtsp links from any course of webcast@berkeley, and dump the stream with mplayer. I thought it was pretty handy.
Simple Twisted Application for Fun <strikethrough>and Profit</strikethrough>
I really enjoyed the series Firefly that aired a few years back. I've enjoyed it a few more times since. I'm really looking forward to the upcoming movie. So much so, in fact, that I even wanted to try to get into one of the pre-screenings. There's a website that announces dates and locations for pre-screenings. Being pretty lazy and also forgetful, I can't be bothered to actually check the website frequently enough to notice when new tickets are available before they are all sold out. Fortunately, being a programmer, I can simply bend a minion to my will. I whipped up this quickie to do the checking for me:
The took about 10 minutes to write and even worked on the 2nd try (the first try I forget recipient addresses had to be a list and gave it a string instead). Then it ran for 21 days before sending me an email a few days ago! Alas, I don't live in Great Britain, or even Europe. It has also, of course, sent out an email once each time one of the listed theaters sells out. And now they're all sold out. :( Given the short period of time remaining before the probable release date, it seems unlikely there will be any more pre-screenings Handy Bookmarklet(zz)This is not my first time seeing this kind of ff bookmark hacks, first it's some perldoc tricks, I copy the dict one here in case I forget (my memery sucks). Maybe latter if i had time, i will copy those perldoc tricks from Perl Hacks.
Also from http://jcalderone.livejournal.com/
Handy Bookmarklet
I cannot present this material in the manner which I desire. LiveJournal scrubs links, removing important structure. So, instead of one instruction, here are eight:
Maybe this is convenient for something. However, it does demonstrate document range selections, which have some other interesting uses. For example, I would like someone to write a Mantissa application which tracks quotes this way. People should be able to vote for quotes they find on the web. The quote can easily be selected and submitted to the server for tracking. The server can keep running tallies for various time periods and report them. It can also do things like indicate the first reporter of a quote, long-persisting quotes, etc. Communities like comp.lang.python should find this useful, as people are always responding to posts with messages that consist of nothing more than "+1 QOTW". This would be an extremely trivial Mantissa application. Who would like to write it? Props to radix for reminding me that dict.org is way better than dictionary.com. 2006/6/26 Limiting Parallelism(zz)This is something i saw on the planet twisted, and Jp Calderone is one of the twisted guys. I want to "steal" some of his interesting stuffs and put it here.
These materials are all from his blog here http://jcalderone.livejournal.com/.
This one particular is about one cool way to limit how much parallelism you run on, much good looking than the one I used to hardcoded.
Limiting Parallelism Concurrency can be a great way to speed things up, but what happens when you have too much concurrency? Overloading a system or a network can be detrimental to performance. Often there is a peak in performance at a particular level of concurrency. Executing a particular number of tasks in parallel will be easier than ever with Twisted 2.4 and Python 2.5: from twisted.internet import defer, task
def parallel(iterable, count, callable, *args, **named):
coop = task.Cooperator()
work = ((yield callable(elem, *args, **named)) for elem in iterable)
return defer.DeferredList([coop.coiterate(work) for i in xrange(count)])
Here's an example of using this to save the contents of a bunch of URLs which are listed one per line in a text file, downloading at most fifty at a time: from twisted.python import log
from twisted.internet import reactor
from twisted.web import client
def download((url, fileName)):
return client.downloadPage(url, file(fileName, 'wb'))
urls = [(url, str(n)) for (n, url) in enumerate(file('urls.txt'))]
finished = parallel(urls, 50, download)
finished.addErrback(log.err)
finished.addCallback(lambda ign: reactor.stop())
reactor.run()
RailsConf2006 ChicagoI think today is day 3, there's some guy taking notes and dairy like about how the RailsConf goes, it's on http://www.oreillynet.com/ruby/blog/. Sounds interesting, but seems they will charge the audio and presentation for $50, wtf. 2006/6/25 Profiling and Optimizing PythonIn this article, the author showed us how to use the profiling tools in the standard library with his own project as an example. It really clearly show the basic steps you have to go through when you find yourself saying, this code runs too long, I got to get it run faster. Why, We like fast, that's why we learn algorithm, that's why we like to play nfs:)
Profiling and Optimizing Pythonby Jeremy Jones12/15/2005 Most of the time, code that we write doesn't have to perform as fast as if we wrote it in C. Most of the time, the first pass at writing it is "fast enough" and we don't have to optimize--but there are times when a piece of code just has to meet a certain standard of performance. For those "it's gotta run like a scalded dog" moments, the Python profiler may lend some assistance. The Python standard library provides three libraries for performing code profiling: Profiling CodeOne of the frequently repeated mantras I hear from Python veterans is, "Premature optimization is the root of all evil" (a quote from Donald Knuth, which apparently originated from Tony Hoare). The Pythonic approach to writing code that performs acceptably is:
The example code I use in these examples comes from ediplex. ediplex is an open source project centered around parsing and handling EDI data. For people who don't know, EDI stands for Electronic Data Interchange and is a generic term used as a general umbrella for the means used to trade electronic business documents between two business partners. I profiled my EDI parser with
calling aggregate_parser.py from the command line and passing the appropriate parameters. Interestingly, running this with
Here is a run on the command line with the profiler:
This run with the
The Interpreting OutputThe statistical output shows the number of times a method was called ( I sorted the statistics on the I should now ask the question, Is it fast enough? My answer is, Probably, but faster would be nice. It can parse about 300 2K interchanges per second on my laptop, (about 440 per second if I don't take into consideration Python interpreter startup, library imports, and such), which is actually pretty respectable, but I would like to trim whatever time I can. The profiler has pointed out a couple of glaring architectural deficiencies, so some changes are in order. I just mentioned the parser's performance without Python interpreter startup and library imports. The
in my
Finding and Fixing BottlenecksA primer on X12 EDI would probably be helpful at this point. EDI documents are character-delimited text files, which are more machine-readable than human-readable. Each document is made up of one interchange; each interchange is made up of a series of segments that roughly equate to database records; each segment is made up of elements that roughly equate to database columns. The first few characters of each segment (sometimes called the tag), up to the element-separating delimiter, declares what type of segment it is. The first segment in an X12 interchange has an ISA tag. The last segment in an X12 interchange has an IEA tag.First problem: when ediplex parses an EDI document, it first determines whether it has a valid header segment, then proceeds to look for each individual segment and determines whether that segment is the end segment. This is really inefficient and results in an inordinate number of calls to the Second problem: I have rearchitected ediplex to work more iteratively rather than relying on callbacks. I have also reduced the granularity of what the parser is interested in on its first pass. Now when ediplex parses a file of EDI interchanges, it identifies the header segment, then immediately starts looking for the footer segment rather than each body segment. When it finds a full interchange, it yields a parser object, which can, in turn, return an iterator for the interchange's segments. ReprofilingThe results were surprising. I was hoping for a 50 to 100 percent performance improvement. Here is a
Here is the parser run with the
Without the profiler, this is a performance boost of about 7X. Removing the Python interpreter startup time, library imports, and such from both sides of the equation, this was actually a performance boost of 10X. The
The profiler shows far fewer methods than before optimizing things. The majority of time is now in methods that actually do things rather than no-op or state transitions. All of the features that had no-op placeholders are now gone and will occur only when a user decides he needs to call them. The profiler also shows far fewer method calls. Before, there were two methods, I heavily optimized the method that searched for the end of an EDI interchange (now called Just for the sake of comparison, here is the
ConclusionI have attempted to live by the "premature optimization" motto with the activity surrounding this article. I spent very little time up front trying to predict where my code would run slow, and made very few concessions to preemptively make it run faster. I simply used the Jeremy Jones is a software engineer who works for The Weather Channel. His weapon of choice is Python. 2006/6/24 离别的七月晚上和几个好友吃散伙饭,他们很快就要离开北京了,我很窝囊的什么离别的话也没说出口,在这里祝福他们一路走好
永远都无法忘记那年的一系列组图,每次看都很伤感
因为离别,因为自己的毫无脸面的离开PKU,因为生活的所有碎片没有丝毫像对的方向发展的趋势,因为... |
|
|